XML : how to parse xml in java using Google Geocode

The code

  try{         URL url = new URL("http://maps.googleapis.com/maps/api/geocode/xml?address=Riyadh&sensor=false");         BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));         String line;          while(null != (line = reader.readLine())){              System.out.println(line);           }            }catch(Exception e){            e.printStackTrace();                 }    

The output:

  Server is Ready....        <?xml version="1.0" encoding="UTF-8"?>      <GeocodeResponse>       <status>OK</status>       <result>        <type>locality</type>        <type>political</type>        <formatted_address>Riyadh Saudi Arabia</formatted_address>        <address_component>         <long_name>Riyadh</long_name>         <short_name>Riyadh</short_name>         <type>locality</type>         <type>political</type>        </address_component>        <address_component>         <long_name>Riyadh Province</long_name>         <short_name>Riyadh Province</short_name>         <type>administrative_area_level_1</type>         <type>political</type>        </address_component>        <address_component>         <long_name>Saudi Arabia</long_name>         <short_name>SA</short_name>         <type>country</type>         <type>political</type>        </address_component>        <geometry>         <location>          <lat>24.7135517</lat>          <lng>46.6752957</lng>         </location>         <location_type>APPROXIMATE</location_type>         <viewport>          <southwest>           <lat>24.2939113</lat>           <lng>46.2981033</lng>          </southwest>          <northeast>           <lat>25.1564724</lat>           <lng>47.3469543</lng>          </northeast>         </viewport>         <bounds>          <southwest>           <lat>24.2939113</lat>           <lng>46.2981033</lng>          </southwest>          <northeast>           <lat>25.1564724</lat>           <lng>47.3469543</lng>          </northeast>         </bounds>        </geometry>        <place_id>ChIJmZNIDYkDLz4R1Z_nmBxNl7o</place_id>       </result>      </GeocodeResponse>    

I'm trying to use Google geocode API to get the coordinates of a city, but I'm facing problem that how can i parse the XML response to retrieve the longitude and the latitude of the city enclosed within the (<location></location>) section.

No comments:

Post a Comment