• 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 and namespace problem

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am trying to query a xml file using xPath, but I am having some problem as I guess the xml file has namespace declarations.

I am trying to get the text of the element called: /Invoice/com:ID, but I just get a nullpointer.

Here is my code:


Here is the XML file:



Does anyone know what I do wrong?

Thanks in advance...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're nearly there.

The problem is that XPath 1.0 doesn't understand the default namespace that is declared as:

xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/testpie/"

Try specifying a prefix for this namespace instead such as:

Namespace ns_com = Namespace.getNamespace("def", "xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/testpie/");

You should then be able to access the node using the following:

org.jdom.xpath.XPath xp = org.jdom.xpath.XPath.newInstance("/def:Invoice/com:ID");
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Phil,

Thanks for your reply.

I tried the following code but it still doesn�t work?! I am wondering if I have to to declare all the namespaces or just the one I want to access? Anyway I have tried to declare all namespaces but without a result.



Here is the XML code:
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hye,
Are you find a solution?
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The revised code segment as posted in posted Friday, May 30, 2008 5:46 PM should be working for the purpose. If it does not work out rightaway, it is only because of the conditioning where the segment is expected to execute, such as defective classpath or import statements or exception throwing...
reply
    Bookmark Topic Watch Topic
  • New Topic