【发布时间】:2011-11-16 06:04:42
【问题描述】:
我尝试使用 xpath 解析soap响应,位于响应消息的一些代码下方。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:Get__CompIntfc__CI_PERSONAL_DATAResponse
xmlns:ns1="http://xmlns.oracle.com/Enterprise/Tools/schemas/M985361.V1">
<ns1:PROP_EMPLID>AA0001</ns1:PROP_EMPLID>
<ns1:PROP_LAST_NAME>Adams</ns1:PROP_LAST_NAME><ns1:PROP_FIRST_NAME>Kimberly</ns1:PROP_FIRST_NAME>
</ns1:Get__CompIntfc__CI_PERSONAL_DATAResponse >
</soapenv:Body>
</soapenv:Envelope>
我尝试像...一样解析它
DocumentBuilderFactory domFactory =DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.writeTo(out);
InputStream is = new ByteArrayInputStream( out.toByteArray() );
Document doc = builder.parse( is );
XPathExpression expr = xpath.compile("//ns1:PROP_EMPLID/text()");
Object res = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) res;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
它没有给出所需的值“AA0001”
但是当我使用xpath.compile("//*/text()") 时,它会正确打印所有文本节点值。
请告诉我问题出在哪里,因为我想要响应中的一些特定值而不是所有文本值。
【问题讨论】:
-
你写错了节点的名字(你在里面包含了命名空间)。检查这个问题:stackoverflow.com/questions/112601/…
-
你可以在这里找到答案:finding tags in soap xml directly by name
-
你可能会找到答案嘿嘿:enter link description here