[英]Unable to get node value of XML using XPath
我的XML如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope`enter code here`
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<platformMsgs:documentInfo
xmlns:platformMsgs="urn:messages_2015_1.platform.webservices.netsuite.com">
<platformMsgs:nsId>WEBSERVICES_3479023</platformMsgs:nsId>
</platformMsgs:documentInfo>
</soapenv:Header>
<soapenv:Body>
<addListResponse
xmlns="">
<platformMsgs:writeResponseList
xmlns:platformMsgs="urn:messages_2015_1.platform.webservices.netsuite.com">
<platformCore:status isSuccess="true"
xmlns:platformCore="urn:core_2015_1.platform.webservices.netsuite.com"/>
<platformMsgs:writeResponse>
<platformCore:status isSuccess="false"
xmlns:platformCore="urn:core_2015_1.platform.webservices.netsuite.com">
<platformCore:statusDetail type="ERROR">
<platformCore:code>DUP_ENTITY</platformCore:code>
<platformCore:message>This entity already exists.</platformCore:message>
</platformCore:statusDetail>
</platformCore:status>
</platformMsgs:writeResponse>
</platformMsgs:writeResponseList>
</addListResponse>
</soapenv:Body>
</soapenv:Envelope>
為了使用XPath獲取價值,我編寫了以下類:
package com.scholastic.intl.esb.integration.resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.NamespaceContext;
public class SimpleNamespaceContext implements NamespaceContext {
private final Map<String, String> PREF_MAP = new HashMap<String, String>();
public SimpleNamespaceContext(final Map<String, String> prefMap) {
PREF_MAP.putAll(prefMap);
}
public String getNamespaceURI(String prefix) {
return PREF_MAP.get(prefix);
}
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
public Iterator getPrefixes(String uri) {
throw new UnsupportedOperationException();
}
}
以下代碼應該讀取platformCore的值:message:
String xPathStr = "/soapenv:Envelope/soapenv:Body/addListResponse/platformMsgs:writeResponseList/platformCore:status/platformMsgs:writeResponse/platformCore:status/platformCore:statusDetail/platformCore:message";
XPath xPath = XPathFactory.newInstance().newXPath();
Map<String, String> prefMap = new HashMap<String, String>() {{
put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
put("xsd", "http://www.w3.org/2001/XMLSchema");
put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
put("platformMsgs", "urn:messages_2015_1.platform.webservices.netsuite.com");
put("platformCore", "urn:core_2015_1.platform.webservices.netsuite.com");
}};
xPath.setNamespaceContext(new SimpleNamespaceContext(prefMap));
System.out.println("Expression value: "+xPath.evaluate(xPathStr, new InputSource(new StringReader(netSuiteResponse))));
其中netSuiteResponse是SOAP格式的輸入SOAP消息,但在Sysout語句中沒有打印任何值。
請告訴我這里出了什么問題,以及如何正確地做到這一點。
此致,Anirban。
你不能像這樣處理空的namspace,你可以匹配名稱。
String xPathStr = "/soapenv:Envelope/soapenv:Body/*[name()='addListResponse']"
+ "/platformMsgs:writeResponseList"
+ "/platformCore:status/@isSuccess"
// + "/platformMsgs:writeResponse/platformCore:status/platformCore:statusDetail/platformCore:message";
;
String xPathStr2 = "/soapenv:Envelope/soapenv:Body/*[name()='addListResponse']"
+ "/platformMsgs:writeResponseList"
+ "/platformMsgs:writeResponse"
+ "/platformCore:status"
+ "/platformCore:statusDetail"
+ "/platformCore:message";
;
XPath xPath = XPathFactory.newInstance().newXPath();
Map<String, String> prefMap = new HashMap<String, String>() {
{
put("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
put("xsd", "http://www.w3.org/2001/XMLSchema");
put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
put("platformMsgs", "urn:messages_2015_1.platform.webservices.netsuite.com");
put("platformCore", "urn:core_2015_1.platform.webservices.netsuite.com");
}
};
xPath.setNamespaceContext(new SimpleNamespaceContext(prefMap));
System.out.println(
"Expression value: " + xPath.evaluate(xPathStr, new InputSource(new StringReader(example))));
System.out.println(
"Expression value: " + xPath.evaluate(xPathStr2, new InputSource(new StringReader(example))));
將導致:
Expression value: true
Expression value: This entity already exists.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.