Madhusudan
Madhusudan

Reputation: 4815

How to get Node from XML without considering namespace name in Java?

I am writing a java program in which I am parsing input xml file which looks like this:

...
<ems:DeterminationRequest>
    <ems:MessageInformation>
       <ns17:MessageID xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">1000225404</ns17:MessageID>
       <ns17:MessageTimeStamp xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">2015-07-28T01:17:04</ns17:MessageTimeStamp>
       <ns17:SendingSystem xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">CH</ns17:SendingSystem>
       <ns17:ReceivingSystem xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">LD</ns17:ReceivingSystem>
       <ns17:ServicingFipsCountyCode xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">037</ns17:ServicingFipsCountyCode>
    </ems:MessageInformation>
</ems:DeterminationRequest>
...

Now I am trying to get node "ems:MessageInformation" without considering namespace name "ems". So I tried following lines of code:

Document doc = db.parse(new FileInputStream(new File("D:\\test.xml")));
Node element = doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation").item(0);
System.out.println(element.getNodeName());

But it's giving Null Pointer exception because function is not reading required node. I gone through this link for reference. Can someone tell me what I am doing wrong here?

Upvotes: 3

Views: 5004

Answers (1)

wero
wero

Reputation: 32980

This is an odd/buggy behaviour in den NodeList implementation returned by

doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation")

It allows you to access item(0) but returns a null object. (If you are using a current JDK the NodeList implementation is com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl which lazily loads its items and shows this buggy behaviour).


To prevent the NullPointerException you should first check if the returned NodeList has a length > 0:

NodeList result = doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation");
if (result.getLength() > 0) {
    Node element = (Element)result.item(0);
    ...
}

Then you need to find out why getElementsByTagNameNS does not return the element.

One possible reason could be that you parsed the document without namespace support. The consequence is that the dom elements don't have namespace information and getElementsByTagNameNS fails.

To turn on namespace support use:

DocumentBuilderFactory.setNamespaceAware(true);

Alternatively without namespace support you could search for

NodeList nl = doc.getDocumentElement().getElementsByTagName("ems:MessageInformation");

Upvotes: 3

Related Questions