如何使用 Java 从具有多个命名空间的 XML 获取特定节点?

How to get specific node from XML with several namespaces using Java?

我想验证 XML 它具有预期的节点值。但我无法获得我需要的节点。请帮助我)

这是我的 XML。我想获取这个节点的值 ns3:site

<soap-env:envelope xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap-env:body>
      <ns4:findsiteconfigurationbysmth xmlns:ns3="http://www.testsite.com/common" xmlns:ns2="http://www.testsite.com/plant" xmlns:ns4="someapi:com:plant" xmlns:ns5="someapi:com:reasoncode">
         <ns4:response>
            <ns2:ref>SiteWD:QWERTY</ns2:ref>
            <ns3:site>QWERTY</ns3:site>
            <ns3:description>test description</ns3:description>
            <ns3:timezone>Africa/Abidjan</ns3:timezone>
         </ns4:response>
      </ns4:findsiteconfigurationbysmth>
   </soap-env:body>
</soap-env:envelope>

我知道我必须以某种方式处理名称空间。我在下面的代码中销售了它们。它对我没有帮助。

我试过这种方法

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();

Document myXml = builder.parse(new File(PATH_TO_XML));

NodeList node = myXml.getDocumentElement().getElementsByTagNameNS("http://www.testsite.com/common", "ns3");

node.item(0);

在这种情况下,我的结果是 null

不知何故,我在一行中收到了带有 ns3 命名空间的节点的所有文本值。原来是这样

SiteBO:15EBDS15EBDSAutomation testAfrica/Abidjan

但我无法重现我使用的方法。尽管这不是我要找的)

请帮我看看问题出在哪里。为什么我无法获取节点的确切值?我应该改变什么?

您在调用 getElementsByTagNameNS 时使用了错误的 namespaceURI - 应该是 http://www.testsite.com/common:

public class Scratch2 {
    public static void main(String[] args) throws Exception {
        // @formatter:off

        String xml = "<soap-env:envelope xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + 
                "   <soap-env:body>\n" + 
                "      <ns4:findsiteconfigurationbysmth xmlns:ns3=\"http://www.testsite.com/common\" xmlns:ns2=\"http://www.testsite.com/plant\" xmlns:ns4=\"someapi:com:plant\" xmlns:ns5=\"someapi:com:reasoncode\">\n" + 
                "         <ns4:response>\n" + 
                "            <ns2:ref>SiteWD:QWERTY</ns2:ref>\n" + 
                "            <ns3:site>QWERTY</ns3:site>\n" + 
                "            <ns3:description>test description</ns3:description>\n" + 
                "            <ns3:timezone>Africa/Abidjan</ns3:timezone>\n" + 
                "         </ns4:response>\n" + 
                "      </ns4:findsiteconfigurationbysmth>\n" + 
                "   </soap-env:body>\n" + 
                "</soap-env:envelope>";

        // @formatter:on

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document myXml = builder.parse(new InputSource(new StringReader(xml)));

        // USING THE CORRECT namespaceURI BELOW
        NodeList nodeList = myXml.getElementsByTagNameNS("http://www.testsite.com/common", "site");

        System.out.println(nodeList.item(0)
                                   .getTextContent());

    }
}

产量:

QWERTY