I have following xml and i am going to insert in one custom object's custom field called Formatted_address__c
.
Now, Problem is all Formatted address not inserting, it just taking the first address> please see the code and guide me where i am doing wrong code.
XML :
<GeocodeResponse>
<status>OK</status>
<result>
<type>route</type>
<formatted_address>US-123, United States</formatted_address>
</result>
<result>
<type>route</type>
<formatted_address>VA-123, Virginia, USA</formatted_address>
</result>
<result>
<type>route</type>
<formatted_address>CT-123, Connecticut, USA</formatted_address>
</result>
<result>
<type>route</type>
<formatted_address>CA-123, California, USA</formatted_address>
</result>
</GeocodeResponse>
APEX :
@RestResource(urlMapping='/ReadXMLfromExternalSystem/*')
global class ReadXMLfromExternalSystem {
@HttpGet
global static void parseResponseDom(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/xml?address=123');
req.setMethod('GET');
HttpResponse res = h.send(req);
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode GeocodeResponse = doc.getRootElement();
List<Dom.XmlNode> xrec = GeocodeResponse.getchildelements();
Dom.XmlNode result = GeocodeResponse.getChildElement('result', null);
//Insert from external system
List<XML_Insert__c> xmlinsertList = new List<XML_Insert__c>();
for(Dom.XMLNode child : xrec){
String formatted_address = result.getChildElement('formatted_address', null).getText();
XML_Insert__c xmlinsert = new XML_Insert__c();
xmlinsert.Formatted_address__c = formatted_address;
xmlinsertList.add(xmlinsert);
}
if(xmlinsertList.size() > 0){
insert xmlinsertList;
}
}
}