function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lin Zhang 4Lin Zhang 4 

issue when parsing xml with prefix/namespace in apex class

Hi there,
  I have been trying to parse the following xml data in apex class, but alway got issue.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:enterprise.soap.sforce.com">
   <soapenv:Header>
      <LimitInfoHeader>
         <limitInfo>
            <current>1699</current>
            <limit>8263200</limit>
            <type>API REQUESTS</type>
         </limitInfo>
      </LimitInfoHeader>
   </soapenv:Header>
   <soapenv:Body>
      <createResponse>
         <result>
            <id>0C9170000008OmqCAE</id>
            <success>true</success>
         </result>
         <result>
            <id>0C9170000008OmrCAE</id>
            <success>true</success>
         </result>
      </createResponse>
   </soapenv:Body>
</soapenv:Envelope>

The testing code I am using for testing is the following, but responseCreateResponse is alway null when running the code in developer console. I am wondering if I missed anything in my code.
//string xmlString = '<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:enterprise.soap.sforce.com"><soapenv:Header><LimitInfoHeader><limitInfo><current>1699</current><limit>8263200</limit><type>API REQUESTS</type></limitInfo></LimitInfoHeader></soapenv:Header><soapenv:Body><createResponse><result><id>0C9170000008OmqCAE</id><success>true</success></result><result><id>0C9170000008OmrCAE</id><success>true</success></result></createResponse></soapenv:Body></soapenv:Envelope>';
       Dom.Document doc = new Dom.Document();
       doc.load(xmlString);

       Dom.XMLNode responseEnvelope = doc.getRootElement();
       String soapenv = 'http://schemas.xmlsoap.org/soap/envelope/';
       if (responseEnvelope != null) {
           system.debug('responseEnvelope: '+responseEnvelope);
           Dom.XMLNode responseBody = responseEnvelope.getChildElement('Body', soapenv);
           if (responseBody != null) {
               system.debug('responseBody: '+responseBody);
               Dom.XMLNode responseCreateResponse = responseBody.getChildElement('createResponse', null);
               if (responseCreateResponse != null){
                   system.debug('responseCreateResponse: '+responseCreateResponse);
                  for(Dom.XMLNode child : responseCreateResponse.getChildElements()) {
                       //System.debug(child.getText());
                       system.debug('id: ' + child.getChildElement('id', null).getText());
                       system.debug('success: ' + child.getChildElement('success', null).getText()); 
                   }                
               }
           }   
       }

 
sandeep@Salesforcesandeep@Salesforce
Hi Lin,

Here Salesforce provides a very easy way of parsing a xml using class "Xmlstreamreader". Here is an example of doing it.
 
Xmlstreamreader reader = new Xmlstreamreader(xmlBody);
and then you need to write a logic in apex to just traverse response of previous method and get the desired result.
while(reader.hasNext()) {
    if (reader.getEventType() == XmlTag.START_ELEMENT) {
        if ('Envelope' == reader.getLocalName()) {
            System.debug(reader.getAttributeValue(null, 'name'));
        }
    }
    reader.next();
}
visit below like to know what all useful methods are given:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_xml_XmlStream_reader.htm

In case of further query please let me know.

Thanks
Sandeep Singhal
http://www.codespokes.com/