Friday, May 1, 2015

Parse Google Geocode XML

function parseXml(xml) { // CURRENT EDIT  
 $(xml).find("address_component").each(function() {
    if($(this).text().indexOf("locality")!= -1) {
        city =$(this).find('long_name').text();
    }
    if($(this).text().indexOf("administrative_area_level_1")!= -1) {
        adminLevel =$(this).find('long_name').text();
    }
    if($(this).text().indexOf("country")!= -1) {
       country =$(this).find('long_name').text();  
    }
   console.log($(this).text()); // just to see them all
 });
  
  console.log(city + " " + adminLevel + " " + country);
  $('#test').text(city + " " + country);

}




<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>route</type>
  <formatted_address>Nnewi-Okigwe Road, Amichi, Nigeria</formatted_address>
  <address_component>
   <long_name>Nnewi-Okigwe Road</long_name>
   <short_name>Nnewi-Okigwe Rd</short_name>
   <type>route</type>
  </address_component>
  <address_component>
   <long_name>Amichi</long_name>
   <short_name>Amichi</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Anambra</long_name>
   <short_name>Anambra</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Nigeria</long_name>
   <short_name>NG</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>5.9868133</lat>
    <lng>6.9893765</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>5.9843302</lat>
     <lng>6.9857796</lng>
    </southwest>
    <northeast>
     <lat>5.9894128</lat>
     <lng>6.9930699</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>5.9843302</lat>
     <lng>6.9857796</lng>
    </southwest>
    <northeast>
     <lat>5.9894128</lat>
     <lng>6.9930699</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJb1nthTaiQxAREtwqA9BC5k0</place_id>
 </result>
 <result>
  <type>locality</type>
  <type>political</type>
  <formatted_address>Igbo Ukwu, Nigeria</formatted_address>
  <address_component>
   <long_name>Igbo Ukwu</long_name>
   <short_name>Igbo Ukwu</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Anambra</long_name>
   <short_name>Anambra</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Nigeria</long_name>
   <short_name>NG</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>6.0166670</lat>
    <lng>7.0166670</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>5.9819729</lat>
     <lng>6.9909096</lng>
    </southwest>
    <northeast>
     <lat>6.0420653</lat>
     <lng>7.0363998</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>5.9819729</lat>
     <lng>6.9909096</lng>
    </southwest>
    <northeast>
     <lat>6.0420653</lat>
     <lng>7.0363998</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJvehBRPuhQxARwZIdLwwUaj0</place_id>
 </result>
 <result>
  <type>administrative_area_level_1</type>
  <type>political</type>
  <formatted_address>Anambra, Nigeria</formatted_address>
  <address_component>
   <long_name>Anambra</long_name>
   <short_name>Anambra</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Nigeria</long_name>
   <short_name>NG</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>6.2757656</lat>
    <lng>7.0068393</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>5.6926150</lat>
     <lng>6.6130860</lng>
    </southwest>
    <northeast>
     <lat>6.7795999</lat>
     <lng>7.3559340</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>5.6926150</lat>
     <lng>6.6130860</lng>
    </southwest>
    <northeast>
     <lat>6.7795999</lat>
     <lng>7.3559340</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJ-cKEFhqFQxAR0LT76AGtYs8</place_id>
 </result>
 <result>
  <type>country</type>
  <type>political</type>
  <formatted_address>Nigeria</formatted_address>
  <address_component>
   <long_name>Nigeria</long_name>
   <short_name>NG</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>9.0819990</lat>
    <lng>8.6752770</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>4.2698571</lat>
     <lng>2.6769320</lng>
    </southwest>
    <northeast>
     <lat>13.8856450</lat>
     <lng>14.6779820</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>4.2698571</lat>
     <lng>2.6769320</lng>
    </southwest>
    <northeast>
     <lat>13.8856450</lat>
     <lng>14.6779820</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJDY2kfa8LThARyAvFaEH-qJk</place_id>
 </result>
</GeocodeResponse>