Link to home
Start Free TrialLog in
Avatar of cgray1223
cgray1223

asked on

XPath Specific Element Search

Hello,

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>

Open in new window



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());
    	    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the same as above, but in a separate class with all imports, etc.

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.StringReader;
import java.util.Iterator;

public class forXPath {
    public static void main(String[] args) {

        String xmlData1 = "<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>";



                InputSource source = new InputSource(new StringReader(xmlData1));

                XPath xPath = XPathFactory.newInstance().newXPath();

                NodeList list = null;
              
                try {
                    xPath.setNamespaceContext(new NamespaceContext() {
                        public String getNamespaceURI(String prefix) {
                            return "http://c1.net.corbis.com/";
                        }
                        public String getPrefix(String namespaceURI) {
                            return "ns";
                        }
                        public Iterator getPrefixes(String namespaceURI) {
                            throw new UnsupportedOperationException("Not supported yet.");
                        }
                    });

                   // list = (NodeList) xPath.evaluate("(//ns:URL128/@Value)|(//ns:ImageUID/@Value)", source, XPathConstants.NODESET);

                     list = (NodeList) xPath.evaluate("(//ns:sessionUID)|(//ns:eNewSTok)",  source, XPathConstants.NODESET);

                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    ex.printStackTrace();
                }
                for (int i = 0; i < list.getLength(); i++) {
                    System.out.println(list.item(i).getTextContent());
                }



    }


}

Open in new window



{f7be62e6-0bf5-44a7-bf77-5ab2bf307a23}
JdKVwENgF7SbdBX2x4R+BTA/WiJatMpCJvFckhNtzbx+WZ8OqmSu+fzD36XL4irDsbp69O8YioZl6iYcwrui6NWo6dBh7YCf18A4c4Ry3nFWLpBkUt35sQmBcON1kD79+1lvdJNZrzKOQIDo3Qs/ogb95aVrp7TAgjIkugti3Q0=

Open in new window