这是我的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);

关于java - Java XML Dom文档getElementsByTagNameNS返回空的NodeList,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37556829/

10-09 05:47
查看更多