0

I'm trying to get the sessionUID and eNewSTok XML elements in the below XML String using XPath, but I can't determine the XPath expression. The NodeList is empty. Looking at the response XML I don't think I need to declare a namespace for XPath. Any suggestions?

response 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 code:

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());
        }
c12
  • 9,557
  • 48
  • 157
  • 253

1 Answers1

0

You need to specify the namespaces for XPathFactory. Look How to use XPath on xml docs having default namespace

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327