gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:19:08 24 4
gpt4 key购买 nike

我正在使用 Xerces 解析器来尝试解析以下 XML 片段。当我使用 findElementsByTagName 方法查找 Circle 时,我可以找到“location-info”元素,我得到一个空的 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>

以下是我的代码,它试图从这个 XML 中获取“Circle”标签

    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 关于 namespace 的完整 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.

最佳答案

当您调用 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);

或者,您可以只使用(没有 namespace )完整的限定名称:

 getElementsByTagName("gs:Circle");

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

关于使用 Xerces 的 getElementsByTagName 的 Java 代码不返回子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com