This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes XPath: Check if a node actually exists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath: Check if a node actually exists" Watch "XPath: Check if a node actually exists" New topic
Author

XPath: Check if a node actually exists

Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
Using javax.xml.xpath.XPath...

If I have an XML file like this:



And I run an expression to get the value of a Node which doesn't even exist:



I get an empty string back. Now from that I can't deduce whether there's a tag there but it's got no content in it, or if it's just not there at all. Is there any way of checking if the node exists using an XPath expression or do I have to actually dive into the DOM to do this?

EDIT | Apologies, I've been sitting watching posts in this forum for the past few days and completely forgot to select a more appropriate forum for this question.
[ August 06, 2007: Message edited by: Chris Corbyn ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

XPath is certainly one of the "Related Technologies" in the name of this forum, so this is the best place for your question.

If you are getting an empty string, then you must be issuing your XPath query in such a way that you get a string result. If you change it so that you request a nodeset result, you should get a nodeset with zero nodes in it when there are no nodes matching the expression.
Chris Corbyn
Ranch Hand

Joined: Jan 14, 2007
Posts: 114
Perfect, and so obvious. Doh! I tried BOOLEAN but it always returned true when the node didn't exist. NodeSet should work a treat logically.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XPath: Check if a node actually exists