0

Following is my XML content :

<GeocodeResponse>
    <status>OK</status>
    <result>
        <type>postal_code</type>
        <formatted_address>Mexico city, Mexico, Mexico</formatted_address>
        <address_component>
            <long_name>123456</long_name>
            <short_name>123456</short_name>
            <type>postal_code</type>
        </address_component>
    </result>
</GeocodeResponse>

I written the following service to get the details from external system :

@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=123456');
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();

        //Retrieve the root element for this document.
        Dom.XMLNode address = doc.getRootElement();

        // LINE Number 16
        String long_name = address.getChildElement('long_name', null).getText();
        String short_name = address.getChildElement('short_name', null).getText();

        System.debug('Long Name: ' + long_name);
        System.debug('Short Name: ' + short_name);

        for(Dom.XMLNode child : address.getChildElements()) {
            System.debug(child.getText());
        }
    }
}

I am getting following error on line No. 16

enter image description here

2

Your parsing code does not match the format of your XML. The root node of your XML is the GeocodeResponse, not the address_component that you want the properties from.

You need to transverse the nodes in the document to get down to the level you want.

Dom.Document doc = res.getBodyDocument();

Dom.XMLNode GeocodeResponse = doc.getRootElement();
Dom.XMLNode result = GeocodeResponse.getChildElement('result', null);
Dom.XMLNode address_component = result.getChildElement('address_component', null);

String long_name = address_component.getChildElement('long_name', null).getText();
String short_name = address_component.getChildElement('short_name', null).getText();

This is covered in the documentation for Reading and Writing XML Using the DOM.

Document Example

For the purposes of the sample below, assume that the url argument passed into the parseResponseDom method returns this XML response:

<address>
    <name>Kirk Stevens</name>
    <street1>808 State St</street1>
    <street2>Apt. 2</street2>
    <city>Palookaville</city>
    <state>PA</state>
    <country>USA</country>
</address>

The following example illustrates how to use DOM classes to parse the XML response returned in the body of a GET request:

public class DomDocument {

    // Pass in the URL for the request
    // For the purposes of this sample,assume that the URL
    // returns the XML shown above in the response body
    public void parseResponseDom(String url){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        // url that returns the XML in the response body
        req.setEndpoint(url);
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();

        //Retrieve the root element for this document.
        Dom.XMLNode address = doc.getRootElement();

        String name = address.getChildElement('name', null).getText();
        String state = address.getChildElement('state', null).getText();
        // print out specific elements
        System.debug('Name: ' + name);
        System.debug('State: ' + state);

        // Alternatively, loop through the child elements.
        // This prints out all the elements of the address
        for(Dom.XMLNode child : address.getChildElements()) {
           System.debug(child.getText());
        }
    }
}
  • Actually i am totally newbie for this XML parse. thank you for clear me. Can i use insert call in this salesforce ?? – Nail David Aug 31 '17 at 9:34
  • @NailDavid Yes. Just make sure you bulkify your logic to be on safer side. – Rohit Mourya Aug 31 '17 at 9:40
  • @RohitMourya Okay Thank you very much. i will do it according to my data. :) – Nail David Aug 31 '17 at 9:42
  • @RohitMourya i have another question regarding this. can you help me here or do i need to post another question ? – Nail David Sep 5 '17 at 9:55
  • @NailDavid Better to post it as a another question – Rohit Mourya Sep 5 '17 at 17:15

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.