wood burning stoves*
The moose likes XML and Related Technologies and the fly likes find text in xml using xpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "find text in xml using xpath" Watch "find text in xml using xpath" New topic
Author

find text in xml using xpath

K Srinivasan
Ranch Hand

Joined: Mar 08, 2010
Posts: 34
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>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Mar 08, 2010
Posts: 34
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

Joined: Mar 08, 2010
Posts: 34
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

Joined: Mar 08, 2010
Posts: 34
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

Joined: Mar 08, 2010
Posts: 34
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
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Mar 08, 2010
Posts: 34
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 ....
 
wood burning stoves
 
subject: find text in xml using xpath
 
Similar Threads
How to search content of tags using Java?
Merging repeted elements in XML
Problem in regular expression for matching
Casting Object
Interview String question