我正在尝试使用XPath在下面的XML String中获取sessionUID和eNewSTok XML元素,但我无法确定XPath表达式。 NodeList为空。查看响应XML我认为我不需要为XPath声明一个名称空间。有什么建议吗?
响应XML:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<wsu:Timestamp xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
<wsu:Created>2011-08-12T03:07:19Z</wsu:Created>
<wsu:Expires>2011-08-12T03:12:19Z</wsu:Expires>
</wsu:Timestamp>
</soap:Header>
<soap:Body>
<LoginResponse xmlns="http://c1.net.corbis.com/">
<**sessionUID**>{f7be62e6-0bf5-44a7-bf77-5ab2bf307a23}</sessionUID>
<**eNewSTok**>JdKVwENgF7SbdBX2x4R+BTA/WiJatMpCJvFckhNtzbx+WZ8OqmSu+fzD36XL4irDsbp69O8YioZl6iYcwrui6NWo6dBh7YCf18A4c4Ry3nFWLpBkUt35sQmBcON1kD79+1lvdJNZrzKOQIDo3Qs/ogb95aVrp7TAgjIkugti3Q0=</eNewSTok>
<securityTokenXML><![CDATA[<SecurityToken><ActualMemberUID Scope="Public" Type="Guid" Value="{b7fde077-c13e-40a7-aae9-f18c5a66f3e1}"/><EffectiveMemberUID Scope="Public" Type="Guid" Value="{b7fde077-c13e-40a7-aae9-f18c5a66f3e1}"/><RoleID Scope="Public" Type="Long" Value="49"/><CountryCode Scope="Public" Type="String" Value="US"/><RegionCode Scope="Public" Type="String" Value="CA"/><LanguageID Scope="Public" Type="Long" Value="100958"/><OnyxSessionID Scope="Public" Type="String" Value=""/></SecurityToken>]]></securityTokenXML>
</LoginResponse>
</soap:Body>
</soap:Envelope>
Java代码:
String responseXML = StringEscapeUtils.unescapeXml(sb.toString());
InputSource source = new InputSource(new StringReader(responseXML));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList list = (NodeList)xPath.evaluate("(//sessionUID)|(//eNewSTok)", source, XPathConstants.NODESET);
for (int i = 0; i < list.getLength(); i++)
{
System.out.println(list.item(i).getTextContent());
}