我试图通过Xparser解析SOAP响应中的一个节点,但由于:存在于节点中,我无法访问特定节点。 以下是我的XML:
<?xml version='1.0' encoding='ISO-8859-1'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:processTransactionResponse xmlns:ns="http://service.tpe.tbms.suntecgroup.com"
xmlns:ax212="http://rmi.java/xsd"
xmlns:ax221="http://request.service.commons.tbms.suntecgroup.com/xsd"
xmlns:ax213="http://io.java/xsd"
xmlns:ax222="http://utilities.service.commons.tbms.suntecgroup.com/xsd"
mlns:ax225="http://response.service.commons.tbms.suntecgroup.com/xsd"
xmlns:ax217="http://request.service.tpe.tbms.suntecgroup.com/xsd"
xmlns:ax219="http://response.service.tpe.tbms.suntecgroup.com/xsd">
<ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="ax225:ProcessResponse">
<ax225:abstractServiceObjects xsi:type="ax222:AbstractServiceObject">
<ax222:fieldId>2201</ax222:fieldId>
<ax222:format xsi:nil="true" />
<ax222:operator>=</ax222:operator>
<ax222:type>1</ax222:type>
<ax222:value>Customer ID was Active</ax222:value>
</ax225:abstractServiceObjects>
</ns:return></ns:processTransactionResponse>
</soapenv:Body>
</soapenv:Envelope>
和JAVA代码:
File reposnseFile = new File("J:/SOAP/response.txt");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = (Document) db.parse(reposnseFile);
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
@Override
public Iterator getPrefixes(String arg0) {
return null;
}
@Override
public String getPrefix(String arg0) {
return null;
}
@Override
public String getNamespaceURI(String arg0) {
if("soapenv".equals(arg0)) {
return "http://schemas.xmlsoap.org/soap/envelope/nsreturn/ax225abstractServiceObjects/ax222value";
}
return null;
}
});
XPathExpression expr = xpath.compile("//soapenv:Envelope/soapenv:Body/ns:processTransactionResponse/ns:return/ax222:value");
System.out.println((String) expr.evaluate(doc, XPathConstants.STRING));
}
预期输出:客户ID处于活动状态(节点<ax222:value>
的值)。
你的帮助会很明显。
答案 0 :(得分:0)
首先:你的XML有一个错误,它的xmlns:ax225
其次,由于命名空间,使用它,它可以工作
XPath xpath = XPathFactory.newInstance().newXPath();
expression="//*[local-name()='value']/text()";
String result = xpath.compile(expression).evaluate(document);
System.out.println("RESULT="+result);