How to extract data inside an xml tag with the same name as another tag in python? -


everyone! trying create application uses google's geocode api (xml). xml data working with:

<geocoderesponse>  <status>ok</status>  <result>   <type>establishment</type>   <type>point_of_interest</type>   <type>university</type>   <formatted_address>77 massachusetts ave, cambridge, ma 02139, usa</formatted_address>   <address_component>    <long_name>77</long_name>    <short_name>77</short_name>    <type>street_number</type>   </address_component>   <address_component>    <long_name>massachusetts avenue</long_name>    <short_name>massachusetts ave</short_name>    <type>route</type>   </address_component>   <address_component>    <long_name>area 2/mit</long_name>    <short_name>area 2/mit</short_name>    <type>neighborhood</type>    <type>political</type>   </address_component>   <address_component>    <long_name>cambridge</long_name>    <short_name>cambridge</short_name>    <type>locality</type>    <type>political</type>   </address_component>   <address_component>    <long_name>middlesex county</long_name>    <short_name>middlesex county</short_name>    <type>administrative_area_level_2</type>    <type>political</type>   </address_component>   <address_component>    <long_name>massachusetts</long_name>    <short_name>ma</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>02139</long_name>    <short_name>02139</short_name>    <type>postal_code</type>   </address_component>   <geometry>    <location>     <lat>42.3600910</lat>     <lng>-71.0941600</lng>    </location>    <location_type>rooftop</location_type>    <viewport>     <southwest>      <lat>42.3587420</lat>      <lng>-71.0955090</lng>     </southwest>     <northeast>      <lat>42.3614400</lat>      <lng>-71.0928110</lng>     </northeast>    </viewport>   </geometry>   <place_id>chijh2oa9apw44krpcais6wo4na</place_id>  </result> </geocoderesponse> 

i trying work through xml data extract county:

<address_component>    <long_name>middlesex county</long_name>    <short_name>middlesex county</short_name>    <type>administrative_area_level_2</type>    <type>political</type>   </address_component> 

however, other tags in xml data use same name of "address_component" , "long_name". due fact there no attributes associated these tags, can't find specific data want. can please me how go through xml data using python , find exact data need despite fact tags have same name?

if intention getting address_component subelement type=administrative_area_level_2, can iterate xml , select desired element:

import xml.etree.elementtree et root = et.fromstring("your xml string")  def find_by_tag(tag, add_type= "administrative_area_level_2"):     address in root.iter("address_component"):         if address.find("type").text == add_type:             return address.find(tag).text     return none 

you can long_name using function find_by_tag:

find_by_tag("long_name") ## 'middlesex county' 

or other tags, e.g.:

find_by_tag("short_name") ## 'middlesex county' find_by_tag("short_name", "postal_code") ## '02139' 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -