使用Xerces的getElementsByTagName的Java代码不返回子节点

时间:2014-04-30 14:03:43

标签: java xml xerces

我在尝试解析以下XML代码段时使用Xerces解析器。我可以找到" location-info"当我使用findElementsByTagName方法查找Circle时,我得到一个空的NodeList。有人可以检查一下,看看我做错了吗?

<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>

以下是我的代码,试图获得&#34; Circle&#34;标记出这个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));
    }

此代码的输出为:

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

我做错了什么?在此先感谢您的帮助!

在guido关于命名空间的完整URI

的评论之后更新
...
    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));
    }

输出仍然相同:

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

1 个答案:

答案 0 :(得分:1)

当你调用getElementsByTagNameNS时,你应该指定命名空间的URI,而不是xml中使用的前缀,所以:

getElementsByTagNameNS("gs", CIRCLE);

应该是:

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

因为gs:Circle元素是在名称空间URI下定义的:

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

要使名称空间起作用,您需要为它设置工厂:

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

或者,您可以简单地使用(没有名称空间)完整限定名称:

 getElementsByTagName("gs:Circle");

注意:另请注意,您的问题中的xml无效,因为它缺少结束根元素</urn:locationResponse>