我有一个 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。
谢谢