2016-04-10 17 views
0

kodtarihinde Geocode

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(); 
       } 

çıkışı: Bir şehrin koordinatlarını almak için Google coğrafi kod API kullanmaya çalışıyorum

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> 

ama '(<location></location>) bölümünde bulunan enlem ve boylamı almak için XML yanıtını nasıl ayrıştıracağım sorunuyla karşılaşıyorum.

cevap

0

Hiyerarşik bir şekilde bir şeye erişmek istiyorsanız, xml ayrıştırma için org.w3c.dom. * Java kitaplığını veya JDOM ... 'ı kontrol edin.

Daha fazla seçenek için lütfen XML Parsers in JAVA'a bakın.

Kodu uyguladıktan sonra, sadece "konum" öğesi için çağrı yapın ve çocuklarını yineleyin ve istediğiniz değerleri alın.

String xmlRecords = "<data><employee><name>A</name>" 
    + "<title>Manager</title></employee></data>"; 

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(xmlRecords)); 

Document doc = db.parse(is); 
NodeList nodes = doc.getElementsByTagName("employee"); 

for (int i = 0; i < nodes.getLength(); i++) { 
    Element element = (Element) nodes.item(i); 

    NodeList name = element.getElementsByTagName("name"); 
    Element line = (Element) name.item(0); 
    System.out.println("Name: " + getCharacterDataFromElement(line)); 

    NodeList title = element.getElementsByTagName("title"); 
    line = (Element) title.item(0); 
    System.out.println("Title: " + getCharacterDataFromElement(line)); 
} 

} 
String xmlRecords = "<data><employee><name>A</name>" 
    + "<title>Manager</title></employee></data>"; 

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
InputSource is = new InputSource(); 
is.setCharacterStream(new StringReader(xmlRecords)); 

Document doc = db.parse(is); 
NodeList nodes = doc.getElementsByTagName("employee"); 

for (int i = 0; i < nodes.getLength(); i++) { 
    Element element = (Element) nodes.item(i); 

    NodeList name = element.getElementsByTagName("name"); 
    Element line = (Element) name.item(0); 
    System.out.println("Name: " + getCharacterDataFromElement(line)); 

    NodeList title = element.getElementsByTagName("title"); 
    line = (Element) title.item(0); 
    System.out.println("Title: " + getCharacterDataFromElement(line)); 
} 

} 
public static String getCharacterDataFromElement(Element e) { 
Node child = e.getFirstChild(); 
if (child instanceof CharacterData) { 
    CharacterData cd = (CharacterData) child; 
    return cd.getData(); 
} 
return ""; 
} 
} 
: Burada

bir örnek
İlgili konular