1

Sometimes I have an XML:

<resource 
    xsi:schemaLocation="http://www.test.com/resource http://test.com/schema/resource 
    xmlns="http://www.test.com/resource"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</resource>

and sometimes w/o the namespace URIs, i.e.

<resource>
...
</resource>

How to make fetch all child elements of resource no matter the namespace URIs?

I am using nu.xom.Element to handle elements and I build a nu.xom.Document this way:

nu.xom.Builder parser = new nu.xom.Builder();
return parser.build( new ByteArrayInputStream(xmlString.getBytes()) );
Yanko
  • 784
  • 8
  • 16

1 Answers1

0

You haven't indicated whether <resource> is the root element. If it is then it's trivial to find. if not, find all <resource> elements by:

Nodes resourceNodes = yourDoc.query("//*[local-name()='resource']");
for (int i = 0; i < resourceNodes .size(); i++) {
    Element resource = (Element) resourceNodes .get(i);
    Elements childElements = resource.getChildElements();
    // .. etc
}

or directly:

Nodes resourceChildren = yourDoc.query("//*[local-name()='resource']/*");
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217