Aliy
Aliy

Reputation: 217

Ignore namespace to create nodelist in Java

I'm parsing a XML string to generate nodes. Sometimes the tag comes with a namespace & sometimes without namespace. How can I ignore this and

I tried in the following way, but it didnt work.

//NodeList idDetails =  doc.getDocumentElement().getElementsByTagNameNS("*", "details");

NodeList idDetails = doc.getElementsByTagName("ns2:details"); 

Any ideas on how to do it?

Upvotes: 1

Views: 1528

Answers (1)

Optional
Optional

Reputation: 4507

First one shall work.

NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("*", str);

But you have to also call DocumentBuilderFactory.setNamespaceAware(true) for this to work, otherwise namespaces will not be detected.

Upvotes: 2

Related Questions