Question

I'm using Xerces parser in trying to parse the following XML snippet. I can find the "location-info" element by when I use the findElementsByTagName method to find Circle, I am getting an empty NodeList back. Can someone please spot check and see what I am doing wrong?

<urn:locationResponse xmlns:urn="urn:ietf:params:xml:ns:geopriv:held">
<presence entity="pres:www.telecomsys.com" xmlns="urn:ietf:params:xml:ns:pidf"
          xmlns:gp="urn:ietf:params:xml:ns:pidf:geopriv10" xmlns:dm="urn:ietf:params:xml:ns:pidf:data-model"
          xmlns:gs="http://www.opengis.net/pidflo/1.0" xmlns:ca="urn:ietf:params:xml:ns:pidf:geopriv10:civicAddr"
          xmlns:gml="http://www.opengis.net/gml">
    <tuple id="FIRST_LOCATION">
        <status>
            <gp:geopriv>
                <location-info xmlns="urn:ietf:params:xml:ns:pidf:geopriv10">
                    <gs:Circle srsName="urn:ogc:def:crs:EPSG::4326">
                        <gml:pos>00.000000 -00.00000</gml:pos>
                        <gs:radius uom="urn:ogc:def:uom:EPSG::9001">200</gs:radius>
                    </gs:Circle>
                    <gp:confidence>95</gp:confidence>
                </location-info>
                <gp:usage-rules>
                    <gp:retransmission-allowed>yes</gp:retransmission-allowed>
                </gp:usage-rules>
                <gp:method>Derived</gp:method>
            </gp:geopriv>
        </status>
        <timestamp>2001-01-00T00:00Z</timestamp>
    </tuple>
</presence>

Following is my code which attempts to get the "Circle" tag out of this XML

    private static final Log logger = LogFactory.getLog(PIDFLOParser.class);
    private static final String LOCATION_INFO = "location-info";
    private static final String CIRCLE = "Circle";

    // Use of the Document BuilderFactory to create a DocumentBuilder class.
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = fact.newDocumentBuilder();

    Document doc = builder.parse(new InputSource(new StringReader(xmlDoc)));
    doc.getDocumentElement().normalize();

    Node node = doc.getDocumentElement();

    String root = node.getNodeName();
    System.out.println("Root Node: " + root);

    NodeList listResponse = doc.getElementsByTagName(LOCATION_INFO);
    if (listResponse.getLength() == 0) {
        System.out.println(String.format("%s doesn't exist in the Document.", LOCATION_INFO));
    }

    Node firstNode = listResponse.item(0);
    listResponse = ((Element) firstNode).getElementsByTagName(CIRCLE);
    if (listResponse.getLength() == 0) {
        System.out.println(String.format("%s doesn't exist in the Document.", CIRCLE));
    }

    listResponse = ((Element) firstNode).getElementsByTagNameNS("gs", CIRCLE);
    if (listResponse.getLength() == 0) {
        System.out.println(String.format("%s doesn't exist in the Document when searching with namespace.", CIRCLE));
    }

Output of this code is:

    Root Node: urn:locationResponse
Circle doesn't exist in the Document.
Circle doesn't exist in the Document when searching with namespace.

What am I doing wrong? Thanks in advance for help!

Updated after guido's comment about full URI of namespace

...
    private static final String NS_GS = "http://www.opengis.net/pidflo/1.0";
...
    listResponse = ((Element) firstNode).getElementsByTagNameNS(NS_GS, CIRCLE);
    if (listResponse.getLength() == 0) {
        System.out.println(String.format("%s doesn't exist in the Document when searching with namespace.", CIRCLE));
    }

Output is still the same:

Root Node: urn:locationResponse
Circle doesn't exist in the Document.
Circle doesn't exist in the Document when searching with namespace.
Was it helpful?

Solution

When you call getElementsByTagNameNS you should specify the URI for the namespace, not the prefix used in the xml, so:

getElementsByTagNameNS("gs", CIRCLE);

should be:

getElementsByTagNameNS("http://www.opengis.net/pidflo/1.0", CIRCLE);

because gs:Circle element is defined under the namespace URI:

xmlns:gs="http://www.opengis.net/pidflo/1.0"

To make namespaces to work, you need to set the factory for it:

 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
 fact.setNamespaceAware(true);

Or alternatively, you can just use simply (without namespaces) the full qualified name:

 getElementsByTagName("gs:Circle");

Note: also note that your xml is invalid in your question because it is missing the closing root element </urn:locationResponse>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top