![](/img/trans.png)
[英]Batik's SVGDocument.getElementsByTagNameNS returns empty NodeList despite of namespace and tagname being correctly set
[英]Java XML Dom Document getElementsByTagNameNS returns empty NodeList
這是我的XML文檔的樣子-
<Key1>
<ns3:a>true</ns3:a>
<ns3:b>1.0</ns3:b>
<ns3:c>
<ns3:north>13</ns3:north>
<ns3:south>113</ns3:south>
<ns3:west>114</ns3:west>
<ns3:east>172</ns3:east>
</ns3:c>
</Key1>
<Key2>
<ns3:SubKey>
<ns3:a>Hello World</ns3:a>
<ns3:b>0.9</ns3:b>
<ns3:c>
<ns3:north>99</ns3:north>
<ns3:south>17</ns3:south>
<ns3:west>65</ns3:west>
<ns3:east>11</ns3:east>
</ns3:c>
</ns3:SubKey>
</Key2>
這是我的Java代碼-
private Document domDocument;
public XmlDomParser(byte[] inputByteFile) {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
domDocument = documentBuilder.parse(new ByteArrayInputStream(inputByteFile));
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
public NodeList getAllNodesByTagName(String tagName) {
return domDocument.getElementsByTagNameNS("*",tagName);
}
當tagName =“ a”時,getAllNodes返回的NodeList為空。 但是,如果嘗試domDocument.getElementsByTagName(tagName),則會得到2個元素的預期列表。
原來我需要啟用dbFactory.setNamespaceAware(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.