2016-09-18 4 views
0
<GeocodeResponse> 
<status>OK</status> 
<result> 
<type>locality</type> 
<type>political</type> 
<formatted_address>Chengam, Tamil Nadu 606701, India</formatted_address> 
<address_component> 
<long_name>Chengam</long_name> 
<short_name>Chengam</short_name> 
<type>locality</type> 
<type>political</type> 
</address_component> 
<address_component> 
<long_name>Tiruvannamalai</long_name> 
<short_name>Tiruvannamalai</short_name> 
<type>administrative_area_level_2</type> 
<type>political</type> 
</address_component> 
<address_component> 
<long_name>Tamil Nadu</long_name> 
<short_name>TN</short_name> 
<type>administrative_area_level_1</type> 
<type>political</type> 
</address_component> 
<address_component> 
<long_name>India</long_name> 
<short_name>IN</short_name> 
<type>country</type> 
<type>political</type> 
</address_component> 
<address_component> 
<long_name>606701</long_name> 
<short_name>606701</short_name> 
<type>postal_code</type> 
</address_component> 
<geometry> 
<location> 
<lat>12.3067864</lat> 
<lng>78.7957856</lng> 
</location> 
<location_type>APPROXIMATE</location_type> 
<viewport> 
<southwest> 
<lat>12.2982423</lat> 
<lng>78.7832165</lng> 
</southwest> 
<northeast> 
<lat>12.3213030</lat> 
<lng>78.8035583</lng> 
</northeast> 
</viewport> 
<bounds> 
<southwest> 
<lat>12.2982423</lat> 
<lng>78.7832165</lng> 
</southwest> 
<northeast> 
<lat>12.3213030</lat> 
<lng>78.8035583</lng> 
</northeast> 
</bounds> 
</geometry> 
<place_id>ChIJu8JCb3jxrDsRAOfhACQczWo</place_id> 
</result> 
</GeocodeResponse> 

나는 xml을 처음 접했고 xml.etree를 어떻게 처리해야할지 모르겠다. 내가 https://docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml에서 읽을 기본 식료품 유용하지만, 여전히 구조에서 위도와 경도 값을 인쇄 출력하기 위해 고투하고있다 -> location.i 당신은 거의 그것을 가지고이xml.etree.Elementree를 사용하여 xml 태그 사이의 텍스트를 읽는 방법 xml이 복잡한 경우

 with open('data.xml', 'w') as f: 
      f.write(xmlURL.text) 
     tree = ET.parse('data.xml') 
     root = tree.getroot() 
     lat = root.find(".//geometry/location") 
     print(lat.text) 

답변

1

같은 것을 시도했다.

lng = root.find(".//geometry/location/lng") 
print(lng.text) 
>> 78.7957856 
:

lat = root.find(".//geometry/location/lat") 
print(lat.text) 
>> 12.3067864 

동일 물론 lng 간다 : root.find(".//geometry/location")root.find(".//geometry/location/lat")에 변경