1

Hi I am trying to parse below xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <soap:Header>
        <wsa:Action>GetUserResponse</wsa:Action>
        <wsa:MessageID>urn:uuid:d43cb18b-d404-41c1-82c6-ae68b4aa9274</wsa:MessageID>
        <wsa:RelatesTo>urn:uuid:a4cc29d7-06b0-41c9-a289-f1848ae5e96d</wsa:RelatesTo>
        <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
        <wsse:Security>
            <wsu:Timestamp wsu:Id="Timestamp-9baad9b8-1ca9-4676-ae02-67d352eaa385">
                <wsu:Created>2015-10-20T16:14:03Z</wsu:Created>
                <wsu:Expires>2015-10-20T16:19:03Z</wsu:Expires>
            </wsu:Timestamp>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <GetUserResponse xmlns="urn:Cornerstone:ClientDataService">
            <User Id="00u5a01i3vmAM4Lty0h7" Guid="874e89ac-0eeb-42b3-97d0-a9c80a1a522e" Active="true" Absent="false" AllowReconcile="false" Language="" UserTimeZone="" DeepLinkBaseURL="https://cadence.oktapreview.com/app/cornerstone/exk4qwr6soLYOags90h7/sso/saml?RelayState="
                xmlns="urn:Cornerstone:ClientData">
                <Contact>
                    <Name Prefix="" First="Shilpa" Middle="" Last="Patil" Suffix="" />
                    <Email>shilpapatil201010@mailinator.com</Email>
                    <Phone Type="Fax"></Phone>
                    <Phone Type="Work">764372647</Phone>
                    <Address CountryCode="IND">
                        <Line1>92648275</Line1>
                        <City>874287548</City>
                        <State>Chattisgarh</State>
                        <PostalCode>8732584276</PostalCode>
                    </Address>
                </Contact>
                <Organization Approvals="1">
                    <Unit Type="Location">IN</Unit>
                    <Unit Type="Division">IN</Unit>
                    <Unit Type="Company">ES_GENERAL</Unit>
                    <Employment LastHireDate="2015-10-20" />
                    <Approver />
                    <Manager />
                </Organization>
                <Demographic>
                    <Ethnicity />
                </Demographic>
                <Authentication>
                    <Username>shilpapatil201010@mailinator.com</Username>
                </Authentication>
                <Custom>
                    <Field Name="Advanced Node Design">no</Field>
                    <Field Name="Analog/Custom IC Design">no</Field>
                    <Field Name="Company">ABC</Field>
                    <Field Name="Department Code"></Field>
                    <Field Name="Digital IC Design-Encounter"></Field>
                    <Field Name="Digital Implementation">no</Field>
                </Custom>
            </User>
        </GetUserResponse>
    </soap:Body>
</soap:Envelope>

i want to read id from the tag

<User Id="00u5a01i3vmAM4Lty0h7" Guid="874e89ac-0eeb-42b3-97d0-a9c80a1a522e" Active="true" Absent="false" AllowReconcile="false" Language="" UserTimeZone="" DeepLinkBaseURL="https://cadence.oktapreview.com/app/cornerstone/exk4qwr6soLYOags90h7/sso/saml?RelayState="
                    xmlns="urn:Cornerstone:ClientData">

I am parsing in the following way

Dom.Document docx = new Dom.Document();
        docx.load(aboveXmlResponse);
        //Retrieve the root element for this document.
        dom.XmlNode xroot = docx.getrootelement() ;
        dom.XmlNode Body = xroot.getChildElement('Envelope', null)
            .getChildElement('soap:Body', null);
        system.debug('*** body is '+Body);

i am getting null while parsing the xml on every node... Is this right way to parse or am i missing any thing while parsing.

I have got the id , i have missed the name spaces.

The code that worked for me is

String envNs = 'http://schemas.xmlsoap.org/soap/envelope/';
        dom.XmlNode userNode = xroot.getChildElement('Body', envNs).getChildElement('GetUserResponse', 'urn:Cornerstone:ClientDataService').getChildElement('User', 'urn:Cornerstone:ClientData');
 system.debug('*** userNode id'+userNode.getAttributeValue('Id',null));
2

When the XML includes namespaces, you need to supply the namespace as the second argument:

String envNs = 'http://schemas.xmlsoap.org/soap/envelope/';
dom.XmlNode Body = xroot.getChildElement('Envelope', envNs)
        .getChildElement('Body', envNs);

Take care to use the right namespace for each element e.g. for 'GetUserResponse' it is 'urn:Cornerstone:ClientDataService'. See e.g. XML Namespaces for more explanation of the various ways namespaces are defined.

  • this works .. thanks a lot.yes i have missed name spaces – rajasekhar Oct 20 '15 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.