How to extract default namespace value from XML in Java or Xpath?

229 views Asked by At

How do I extract default namespace value from XML in pure Java or Xpath in Java?

<root xmlns="http://root.com/v1" xmlns:other="http://other.com/v2">
   <childs>
   ...
</root>

I supose that the default xml namespace be: xmlns="http://root.com/v1"

But I can have other case where the same XML have a prefix like:

<x:root xmlns:x="http://root.com/v1" xmlns:other="http://other.com/v2">
   <x:childs>
   ...
</x:root>

I'd like just the value inside the "default" xmlns or the value inside xmlns:x that is being used by the root node. I need this value: http://root.com/v1

Thank you very much in advance.

1

There are 1 answers

0
Doug LN On

As @mark61 commented out namespace-uri(/*) works fine. It ignore the prefix and always return the correct value from namespace.