Error
This content is not currently supported on this browser.
+ Start a Discussion
nicloznicloz 

Parse XML from google

Dears

 

Please could you help me to parse a XML code that contain the latitude and longitude of address? Below the XML code I try with the manual but I has for soap and the another example is too simple to parse this complicated XML of google.

 

I want to take the value of latitude and longitude.

 

Thanks in advances

 

<GeocodeResponse> 
 
<status>OK</status>
 
<result>
 
<type>street_address</type>
 
<formatted_address>1600 Amphi Pkwy, Mountain, CA 94043, USA</formatted_address>
 
<address_component>
   
<long_name>1600</long_name>
   
<short_name>1600</short_name>
   
<type>street_number</type>
 
</address_component>
 
<address_component>
   
<long_name>Amphitheatre Pkwy</long_name>
   
<short_name>Amphitheatre Pkwy</short_name>
   
<type>route</type>
 
</address_component>
 
<address_component>
   
<long_name>Mountain View</long_name>
   
<short_name>Mountain View</short_name>
   
<type>locality</type>
   
<type>political</type>
 
</address_component>
 
<address_component>
   
<long_name>San Jose</long_name>
   
<short_name>San Jose</short_name>
   
<type>administrative_area_level_3</type>
   
<type>political</type>
 
</address_component>
 
<address_component>
   
<long_name>Santa Clara</long_name>
   
<short_name>Santa Clara</short_name>
   
<type>administrative_area_level_2</type>
   
<type>political</type>
 
</address_component>
 
<address_component>
   
<long_name>California</long_name>
   
<short_name>CA</short_name>
   
<type>administrative_area_level_1</type>
   
<type>political</type>
 
</address_component>
 
<address_component>
   
<long_name>United States</long_name>
   
<short_name>US</short_name>
   
<type>country</type>
   
<type>political</type>
 
</address_component>
 
<address_component>
   
<long_name>94043</long_name>
   
<short_name>94043</short_name>
   
<type>postal_code</type>
 
</address_component>
 
<geometry>
   
<location>
   
<lat>37.4217550</lat>
   
<lng>-122.0846330</lng>
   
</location>
   
<location_type>ROOFTOP</location_type>
   
<viewport>
   
<southwest>
     
<lat>37.4188514</lat>
     
<lng>-122.0874526</lng>
   
</southwest>
   
<northeast>
     
<lat>37.4251466</lat>
     
<lng>-122.0811574</lng>
   
</northeast>
   
</viewport>
 
</geometry>
 
</result>
</GeocodeResponse>
Best Answer chosen by Admin (Salesforce Developers) 
incuGuSincuGuS

Hi nicloz

 

That was just some code idea, not meant to work copy-pasting :)

My previous code was meant as a help to get started using this xml parsing method, and the dom.Document object.

 

 

 

dom.Document doc = res.getBodyDocument();

dom.XmlNode location = doc.getRootElement().getChildElement('result',null).getChildElement('location',null);

longitude = location.getChildElement('lng', null).getText(); latitude = location.getChildElement('lat', null).getText();

 

 

That should work thought.

 

It goes to the Root element "GeoCodeResponse" , ask for its child "result" and for that node's child named "location".

 

in your structure :

 

<GeocodeResponse> 
  ...

 
<result>
...

<location>

<lat>37.4217550</lat>  
<lng>
-122.0846330</lng>
 </location>
...
</result>
<GeocodeResponse>

As you can see from the code i pasted above, its really straightforward to get the child elements and such.

I havent tested this code with your input or compiled it, it might have some errors, but this should get you going in the right direction.

 

 

Gaston.

 

 

 

All Answers

incuGuSincuGuS

Hey!,

 

Lets assume that on  RES you got the returned HTTPResponse from the callout to google.

 

 

 

String latitude ='';

String longitude ='';


dom.Document doc = res.getBodyDocument();
for(dom.XmlNode node : doc.getRootElement().getChildElements()) {
   if(node.getName()=='location') {
      longitude = node.getChildElement('lng', null).getText();
      latitude = node.getChildElement('lat', null).getText();
   }
}

 

 

That will go through your XML , using dom.Document, and retrieve the node "location".

When it finds <location> , it will ask for the content (text) of its childs "lat" and "lng" , and you will get it.

 

This is from the top of my head, havent compiled it. But it should be good to get you started on XML parsing! :P

 

Let me know if this helped you out, try tweaking it a bit and throwing some System.debugs if it doesnt work "out of the box"

Gaston.

nicloznicloz

Sorry the result is null for latitude and longitude. I check the result and I received the same XML. Waiting for feed back.

 

Below the code:

 

        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://maps.google.com/maps/api/geocode/xml?address=torre+parque+cristal+caracas+venezuela&sensor=false');
        req.setMethod('GET');

         HTTPResponse res = http.send(req);
 //        Dom.Document doc = res.getBodyDocument();
//         Dom.Document doc = res.getBody();
 string longitude;
 string latitude;
 
dom.Document doc = res.getBodyDocument();
for(dom.XmlNode node : doc.getRootElement().getChildElements()) {
   if(node.getName()=='location') {
      longitude = node.getChildElement('lng', null).getText();
      latitude = node.getChildElement('lat', null).getText();
   }
}
  

incuGuSincuGuS

Hi nicloz

 

That was just some code idea, not meant to work copy-pasting :)

My previous code was meant as a help to get started using this xml parsing method, and the dom.Document object.

 

 

 

dom.Document doc = res.getBodyDocument();

dom.XmlNode location = doc.getRootElement().getChildElement('result',null).getChildElement('location',null);

longitude = location.getChildElement('lng', null).getText(); latitude = location.getChildElement('lat', null).getText();

 

 

That should work thought.

 

It goes to the Root element "GeoCodeResponse" , ask for its child "result" and for that node's child named "location".

 

in your structure :

 

<GeocodeResponse> 
  ...

 
<result>
...

<location>

<lat>37.4217550</lat>  
<lng>
-122.0846330</lng>
 </location>
...
</result>
<GeocodeResponse>

As you can see from the code i pasted above, its really straightforward to get the child elements and such.

I havent tested this code with your input or compiled it, it might have some errors, but this should get you going in the right direction.

 

 

Gaston.

 

 

 

This was selected as the best answer
nicloznicloz

Thank you very much for your help, the issue is that apex code manual 10 the examples in that book are very simple. Below the final code.

 

dom.Document doc = res.getBodyDocument();
//geometry
dom.XmlNode location = doc.getRootElement().getChildElement('result',null).getChildElement('geometry',null).getChildElement('location',null);         

longitude = location.getChildElement('lng', null).getText();
latitude = location.getChildElement('lat', null).getText();

 

 

I add the childer node "getChildElement('geometry',null)" to your message and finally I get the result.

 

 

Best Regards

HussainHussain

Hi Nicloz,

I need to Develop geoceding with XML i dont have any knowledge on this can you plese share your code and experience with me ...

plz waiting for your response....

 

 

 

Thanks in Advance...

Balu