kimmyaf, 26.04.2010 00:24:
Hello. I've only done a litte bit of parsing with minidom before but I'm
having trouble getting my values out of this xml. I need the latitude and
longitude values in bold.

I don't see anything 'bold' in your mail, but your example tells me what data you mean.

Here is some untested code using xml.etree.cElementTree:

    import xml.etree.cElementTree as ET
    tree = ET.parse("thefile.xml")
    for tag in tree.getiterator("location"):
        print tag.findtext("lat"), tag.findtext("lng")

Note that cElementTree is both faster and simpler than minidom.

Stefan



<GeocodeResponse>
  <status>OK</status>
  <result>
   <type>street_address</type>
   <formatted_address>50 Oakland St, Wellesley, MA 02481,
USA</formatted_address>
   <address_component>
    <long_name>50</long_name>
    <short_name>50</short_name>
    <type>street_number</type>
   </address_component>
   <address_component>
    <long_name>Oakland St</long_name>
    <short_name>Oakland St</short_name>
    <type>route</type>
   </address_component>
   <address_component>
    <long_name>Wellesley</long_name>
    <short_name>Wellesley</short_name>
    <type>locality</type>
    <type>political</type>
   </address_component>
   <address_component>
    <long_name>Wellesley</long_name>
    <short_name>Wellesley</short_name>
    <type>administrative_area_level_3</type>
    <type>political</type>
   </address_component>
   <address_component>
    <long_name>Norfolk</long_name>
    <short_name>Norfolk</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>02481</long_name>
    <short_name>02481</short_name>
    <type>postal_code</type>
   </address_component>
   <geometry>
    <location>
     <lat>42.3118520</lat>
     <lng>-71.2632680</lng>
    </location>
    <location_type>ROOFTOP</location_type>
    <viewport>
     <southwest>
      <lat>42.3093524</lat>
      <lng>-71.2665476</lng>
     </southwest>
     <northeast>
      <lat>42.3156476</lat>
      <lng>-71.2602524</lng>
     </northeast>
    </viewport>
   </geometry>
  </result>
</GeocodeResponse>


Code:

body = dom.getElementsByTagName('GeocodeResponse')[0]

for item in body.getElementsByTagName('location'):
      lat = item.getAttribute('lat')
      lng = item.getAttribute('lng')

_______________________________________________
XML-SIG maillist  -  XML-SIG@python.org
http://mail.python.org/mailman/listinfo/xml-sig

Reply via email to