• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

find text in xml using xpath

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i am new to xpath.
can i find if there exists text in an xml using xpath
for eg
a xml should be
<a>
<b>abc</b>
<c>def</c>
</a>

i want to find if there is sometext outside the tags
like
<a>asdfsaf
<b>abc</b>
<c>def</c>
</a>
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to me you could evaluate to get a NodeList, then iterate through the NodeList - you will see Text nodes for both the printing characters "asdfsaf" in your example, and the non printing formatting line feeds, spaces, tabs, etc.

Bill
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you want to identify text nodes which are children of the <a> element? (Not ancestors, but children.) And these have to be text nodes which aren't all whitespace?

The first part should be easy -- you were on the wrong track thinking of "tags", whereas when you pose the question in terms of nodes in the XML tree it becomes much more transparent. The "not whitespace" part -- I believe there's an XPath function which normalizes text. So start from there and let us know if you still have problems.
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply..
but couldnt' understand much...guess i need to study xpath a little more,before attempting....

from the first reply i could understand that i'll get a nodelist which will have all the nodes
that is
<a>
asdfsaf
<b>
abc
<c>
def

but how do i get the tag asdfsaf from that ??
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , i tried this...


but in my case it returns

asdfsaf

abc

def

how do i seperate out "asdfsaf" from the other text nodes which can be there ??

thanks
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , i tried this...


but in my case it returns

asdfsaf

abc

def

how do i seperate out "asdfsaf" from the other text nodes which can be there ??

thanks
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i tried this :


this returns all the text nodes..

asdfsaf

abc

def


how do i seperate out asdfsaf from the other text nodes which are valid..(all i am trying to do is findout if there are any extra text nodes other than those which are allowed)
thanks.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the name of the parent element for each TEXT node first.

You really need to get very very familiar with the methods in org.w3c.dom.Node - see the JavaDocs - there is a very useful table giving the kinds of Nodes..

Bill
 
K Srinivasan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi william..
thanks..finally figured that out...
now stuck on how to remove the whitespaces which creep in as text nodes...

any clues would be appreciated

thanks ....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic