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

问题描述 投票:0回答:1

我想验证它预期节点值的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

但我无法重现我使用的方法。即使这不是我想要的)

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

java xml xpath soap xml-namespaces
1个回答
1
投票

你在namespaceURI的调用中使用了错误的getElementsByTagNameNS - 应该是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
© www.soinside.com 2019 - 2024. All rights reserved.