0

我有一个 SOAP 响应如下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<NTService.DisconnectResourceResult     xmlns="http://www.evolving.com/NumeriTrack/xsd">
<retData xmlns="">
<retCode>rcSuccess</retCode>
<retMsg/>
<resErrList/>
</retData>
</NTService.DisconnectResourceResult>
</soapenv:Body>
</soapenv:Envelope>

我不擅长生成 XPATH 查询,但使用 SOAPUI 我能够获取 XPATH 查询以获取retCode如下:

//ns1:NTService.DisconnectResourceResult[1]/retData[1]/retCode[1]/text()

在 Java 中,我试图获取retCode但无法获取输出。

XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new SoapNamespaceContext());

String     sxpath="//ns1:NTService.DisconnectResourceResult[1]/retData[1]/retCode[1]/text()";
System.out.println("sxpath is " + sxpath);
XPathExpression expr;
expr = xpath.compile(sxpath);
System.out.println("expr is " + expr);
Object result;
result = expr.evaluate(sb, XPathConstants.NODESET);
System.out.println("result is " + result);
NodeList nodes = (NodeList) result;
System.out.println("Length of result is " + nodes.getLength());
 for (int i = 0; i < nodes.getLength(); i++) {
     System.out.println(nodes.item(i).getNodeValue()); 
 }

我也设置了命名空间。

public String getNamespaceURI(String prefix) {
        System.out.println("Prefix is " +prefix);
      if (prefix == null) throw new NullPointerException("Null prefix");
        else if ("ns1".equals(prefix)) {
            System.out.println("Returning http://www.evolving.com/NumeriTrack/xsd");
            return "http://www.evolving.com/NumeriTrack/xsd";
                    }
        else if ("n".equals(prefix)) {
            System.out.println("Returning http://schemas.xmlsoap.org/soap/envelope/");
            return "http://schemas.xmlsoap.org/soap/envelope/";
                    }
        else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
        return XMLConstants.NULL_NS_URI;
}

`

谁能建议我从 SOAP 消息响应中获取 retCode。

谢谢

4

1 回答 1

0

这是答案:

            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("D:\\Loic_Workspace\\Test2\\res\\test.xml"));


        System.out.println(doc.getElementsByTagName("retCode").item(0).getTextContent());

您应该使用 .getTextContent() 方法。

将根据需要输出rcSuccess :)

希望对你有帮助

于 2012-12-05T00:46:47.543 回答