+ Start a Discussion
Aurélien.LavalAurélien.Laval 

Parse XML doesn't work

Hello, I try to parse a web service's XML response in reading the documentation (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_xml_dom.htm) but it doesn't work whereas my XML is valid.

 

This is my XML response :

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetConsumerOrdersResponse xmlns="http://tempuri.org/">
         <GetConsumerOrdersResult xmlns:a="http://schemas.datacontract.org/2004/07/FujiTTModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:ConsumerOrders>
               <a:ConsumerOrder>
                  <a:ActionCode i:nil="true"/>
                  <a:Campaign i:nil="true"/>
                  <a:Consumer>
                     <a:Company i:nil="true"/>
                     <a:ConsumerId>e27dc-6e93-483f-bf14-fe01e477fc7b</a:ConsumerId>
                     <a:Email i:nil="true"/>
                     <a:ExternalId i:nil="true"/>
                     <a:FirstName i:nil="true"/>
                     <a:InvoiceAddress>
                        <a:AddressLine1 i:nil="true"/>
                        <a:AddressLine2 i:nil="true"/>
                        <a:AddressLine3 i:nil="true"/>
                        <a:AddressNumber i:nil="true"/>
                        <a:AddressNumberExtension i:nil="true"/>
                        <a:City i:nil="true"/>
                        <a:Company i:nil="true"/>
                        <a:Country i:nil="true"/>
                        <a:FirstName i:nil="true"/>
                        <a:LastName i:nil="true"/>
                        <a:State i:nil="true"/>
                        <a:Title i:nil="true"/>
                        <a:ZipCode i:nil="true"/>
                     </a:InvoiceAddress>
                     <a:Language i:nil="true"/>
                     <a:LastName>BARBARA BABA</a:LastName>
                     <a:ShippingAddress i:nil="true"/>
                     <a:Telephone i:nil="true"/>
                     <a:Title i:nil="true"/>
                  </a:Consumer>
                  <a:Dealer>
                     <a:Address>16, Rue de la merveille</a:Address>
                     <a:City>BOIS</a:City>
                     <a:Country>FR</a:Country>
                     <a:DealerId>8ff5-8715-48e3-9688-723039d3f212</a:DealerId>
                     <a:Email i:nil="true"/>
                     <a:LabCode>33</a:LabCode>
                     <a:Language i:nil="true"/>
                     <a:Name>Imaging France</a:Name>
                     <a:Number>7651</a:Number>
                     <a:ShippingCode>?-0494</a:ShippingCode>
                     <a:Telephone i:nil="true"/>
                     <a:Zipcode>78391</a:Zipcode>
                  </a:Dealer>
                  <a:Discount>0</a:Discount>
                  <a:ExternalId i:nil="true"/>
                  <a:IsMailOrder>false</a:IsMailOrder>
                  <a:LabCode>33</a:LabCode>
                  <a:Make>FFF</a:Make>
                  <a:MessageList i:nil="true"/>
                  <a:OrderSize>30634986</a:OrderSize>
                  <a:ParentFileId>62f1c94d-c967-49c8-8758-e32987422e3</a:ParentFileId>
                  <a:ProductList i:nil="true"/>
                  <a:ProductionOrders>
                     <a:ProductionOrder>
                        <a:BackupPath>\\fujife10\Backup-2013-10-24-55996</a:BackupPath>
                        <a:DateToShip>2013-10-28T17:00:00</a:DateToShip>
                        <a:DeliveryNoteList i:nil="true"/>
                        <a:FileId>62f1c94d-c967-49c8-8758-e32875f4b2e3</a:FileId>
                        <a:IsConfirmed>true</a:IsConfirmed>
                        <a:IsErrorFlagged>false</a:IsErrorFlagged>
                        <a:IsPriced>true</a:IsPriced>
                        <a:IsProduced>true</a:IsProduced>
                        <a:LabDays>0</a:LabDays>
                        <a:MessageList i:nil="true"/>
                        <a:PartNumber>0</a:PartNumber>
                        <a:PartsTotal>0</a:PartsTotal>
                        <a:ProductList i:nil="true"/>
                        <a:ProductionReference>55996</a:ProductionReference>
                        <a:Subcontractor i:nil="true"/>
                        <a:TimeDelivered>0001-01-01T00:00:00</a:TimeDelivered>
                        <a:TimePrices>0001-01-01T00:00:00</a:TimePrices>
                        <a:TimeProduction>0001-01-01T00:00:00</a:TimeProduction>
                        <a:TimeReOrder>0001-01-01T00:00:00</a:TimeReOrder>
                        <a:TimeUpload>0001-01-01T00:00:00</a:TimeUpload>
                        <a:TimesOrdered>0</a:TimesOrdered>
                        <a:TrackingCode i:nil="true"/>
                        <a:TransportCode>0</a:TransportCode>
                        <a:TransportName i:nil="true"/>
                     </a:ProductionOrder>
                  </a:ProductionOrders>
                  <a:SoftwareType>FFFKiosk</a:SoftwareType>
                  <a:Source>FFF/FTP</a:Source>
                  <a:SourceReference>963697</a:SourceReference>
                  <a:Time>2013-10-24T12:31:11.133</a:Time>
                  <a:TimeCompleted>2013-10-28T14:58:14.997</a:TimeCompleted>
                  <a:TimeIntake>2013-10-24T11:07:00</a:TimeIntake>
                  <a:TimeReceived>2013-10-24T12:31:00</a:TimeReceived>
                  <a:VersionNumber i:nil="true"/>
               </a:ConsumerOrder>
            </a:ConsumerOrders>
         </GetConsumerOrdersResult>
      </GetConsumerOrdersResponse>
   </s:Body>
</s:Envelope>

 

And this is my code :

xmlString = 'XML above';
		
Dom.Document doc = new Dom.Document();
doc.load(xmlString);
Dom.Xmlnode envelope = doc.getRootElement();
		
for(integer i = 0; i < envelope.getChildElements().size(); i++){
			
	Dom.XMLNode body = envelope.getChildElements()[i];
			
	for(Dom.XMLNode child : body.getChildElements()){
	   System.debug(child.getName());
	   System.debug('+++++ : ' + child.getText());
	}			
}

  I get only this : 

1
GetConsumerOrdersResponse
+++++ : 

 

What is the problem please?

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma
You will have to traverse each node. Like
envelope.getChildElement('GetConsumerOrdersResponse').getChildElement('GetConsumerOrdersResult').getChildElement('ConsumerOrder').getChildElement('ConsumerOrder')........
Make sure to put the name space

All Answers

Bhawani SharmaBhawani Sharma
You will have to traverse each node. Like
envelope.getChildElement('GetConsumerOrdersResponse').getChildElement('GetConsumerOrdersResult').getChildElement('ConsumerOrder').getChildElement('ConsumerOrder')........
Make sure to put the name space
This was selected as the best answer
Aurélien.LavalAurélien.Laval

Thanks for your answer.

 

Your code doesn't work but I get the consumer with that :

Dom.XMLNode consumer = envelope.getChildElements()[0].getChildElements()[0].getChildElements()[0].getChildElements()[0].getChildElements()[0].getChildElements()[2];

 

It's not beautiful I know but it works, thank, resolved. :)

Bhawani SharmaBhawani Sharma
You will have to make some minor changes in the Code, I provided.
DOM.getChildElement(tagName, namespace) is the correct method. Make sue you retrieve the each node by name and then get data. Here is a sample code.

//Response Node
Dom.XmlNode responseNode = response.getBodyDocument().getRootElement()
.getChildElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/')
.getChildElement('AddCardResponse', 'http://fleetCredit.comdata.com/maintenance/')
.getChildElement('response', null);
Aurélien.LavalAurélien.Laval

It works, thank for your help.