![](/img/trans.png)
[英]Java w3c dom getElementsByTagName does not return any node
[英]Java code using Xerces's getElementsByTagName does not return child node
我在尝试解析以下XML代码段时使用Xerces解析器。 当我使用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关于名称空间的完整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);
或者,您可以简单地(不使用名称空间)使用完全限定名称:
getElementsByTagName("gs:Circle");
注意:还请注意,您的xml在您的问题中无效,因为它缺少结束根元素</urn:locationResponse>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.