Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XPath: Check if a node actually exists

 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic