0

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;
        }
    }
}
1

You were very near to get address field. You just needed to add check for formatted_address node. Refer below code:

Dom.XMLNode GeocodeResponse = doc.getRootElement();
List<Dom.XmlNode> xrec = GeocodeResponse.getChildelements();
List<XML_Insert__c> xmlinsertList = new List<XML_Insert__c>();
for(Dom.XMLNode child : xrec){
    List<Dom.XMLNode> resultList = child.getChildElements();
    System.debug('resultList::'+resultList.size());

    for(Dom.XMLNode result: resultList) {
        if(result.getName() == 'formatted_address') {
            String formatted_address = result.getText();
            System.debug('formatted_address::'+formatted_address);
            XML_Insert__c xmlinsert = new XML_Insert__c();
            xmlinsert.Formatted_address__c = formatted_address;
            xmlinsertList.add(xmlinsert);
        }
    }
}
if(xmlinsertList.size() > 0){
    insert xmlinsertList;
}
  • Thank you :) Can you please tell me when we have to use getChildren() in this case ?? – Nail David Sep 6 '17 at 9:22
  • 1
    @NailDavid Part 1: It's really simple. Try to visualize your XML as a JSON. If your root node contains multiple child nodes like this e.g. <name>ABC</name> then consider it as a single property because it doesn't has any child nodes. And if node has something like this<address> <Street>1</Street><Pincode><123456</Pincode></address> then it is a object (if you compare it with JSON format address = {Stree:1,Pincode:123456}) – Rohit Mourya Sep 6 '17 at 9:39
  • 1
    @NailDavid Part 2: and lastly if you've xml something like this : <address> <Street>1</Street><Pincode><123456</Pincode></address> <address> <Street>1</Street><Pincode><123456</Pincode></address> <address> <Street>1</Street><Pincode><123456</Pincode></address>. Then consider it as an array of objects where address is a object. If you've singular entity then use getChildElement() else use getChildElements(). Hope it helps – Rohit Mourya Sep 6 '17 at 9:39
1
@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);

        system.debug('getting zxml data===>'+xrec);
        //Insert from external system
        List<XML_Insert__c> xmlinsertList = new List<XML_Insert__c>();
        Integer count = 0;
        XML_Insert__c xmlinsert;
        for(Dom.XMLNode child : xrec)
        {
                if(child.getNodeType() == DOM.XMLNodeType.ELEMENT)
                {
                    system.debug('entering here==>'+child.getName());
                    if(child.getName() == 'result')
                    {
                        system.debug('entered after result');
                        List<DOM.XMLNode> inner2 = child.getChildElements();
                        system.debug('inner222===>'+inner2);
                        for(Dom.XMLNode nodess : inner2)
                        {
                            system.debug('entered second for loop'+nodess);
                            if(nodess.getNodeType() == DOM.XMLNodeType.ELEMENT)
                            {
                                system.debug('node names===>'+nodess.getName());
                                if(nodess.getName() == 'formatted_address')
                                {
                                    xmlinsert = new XML_Insert__c();
                                    xmlinsert.Formatted_address__c = nodess.getText();
                                    xmlinsertList.add(xmlinsert);
                                    system.debug('entered formatted address==>'+nodess.getText());
                                }
                            }
                        }

                    }


                }

                count++;
        }
        system.debug('count are===>'+count);
        if(xmlinsertList.size() > 0){
            insert xmlinsertList;
        }
    }
}

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.