• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Stumped with Java xPath problem returning null.

 
Dale Preston
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've spent lot of time this weekend trying to solve my first Java xPath issue - though I'm experienced doing xPath in .Net.

I've created this sample and cleaned up XML file (such as no valid IDs and passwords) for testing:



After my original code kept failing to find the correct results, I created this simple method to try to get the results:



Using the xPathQuery in the sample Java code works if I try it against the same XML in various online xPath test sites and it works if I try it against the same XML in Altova XMLSpy. It just doesn't work in Java. The two commented out lines in the Java code were ust used to validate that the document was loaded properly. getElementsById worked with that query to return a NodeList with all of the datasource elements so I know the Document is loaded ok. I just can't seem to get the xPath to work.

Can anyone find what I'm missing and why this doesn't work for me?

Thanks,

Dale
 
g tsuji
Ranch Hand
Posts: 734
7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.Net and java adapte both a very similar approach to take into account of the namespace(s). In .Net it is XmlNamespaceManager, in java it is NamespaceContext.

[0] If you feel comfortable for this particular xml to bypass the namespace issue, you can write the xpath like this.

[1] To treat properly the namespace use in the xpath expression, you set up its NamespaceContext like this. (ns2 is not generic, you can name the prefix whatever you want, systactically correct of course, as long as there is an agrees between the prefix used in the xpath and the NamespaceContext's namespace uri resolution. It is decoupled from what is used in the xml itself.)
 
Dale Preston
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, g. I really appreciate the help; removing the namespace from the xPath got me past the problem. I don't need the namespace context for this tool but I appreciate your sample on how to properly handle the namespace. I'm going to tuck that away for future reference.

Dale
 
You didn't tell me he was so big. Unlike this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic