Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get namespace of root element

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to get the first namespace value (URI) of the root element:
xmlns="http://rep.oio.dk/ubl/xml/schemas/0p71/testpie/"

The XML document starts with:



I wish to print out the value "http://rep.oio.dk/ubl/xml/schemas/0p71/testpie/", but my code below returns null:



Any help would be appreciated.

Thanks in advance.


 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all, is your parser namespace-aware? By default parsers created by DocumentBuilderFactory (which it looks like you are using that) are not namespace-aware. Check the methods of DocumentBuilderFactory.

Then once you have taken care of that, the method you're looking for is getNamespaceURI().
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, first of all, is your parser namespace-aware? By default parsers created by DocumentBuilderFactory (which it looks like you are using that) are not namespace-aware. Check the methods of DocumentBuilderFactory.

Then once you have taken care of that, the method you're looking for is getNamespaceURI().


Thanks. It works :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic