• 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

XPath: Check if a node actually exists

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 28226
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic