[XML-SIG] parsing XML with minidom

Peter Bigot bigotp at acm.org
Wed Apr 28 01:37:33 CEST 2010


I'd have to concur with that recommendation.  Google is uninterested in
defining schema for their APIs, so you need to process the XML manually and
hope they don't change their interface.

BTW: The lat and lon components of the location are elements, not
attributes.  For minidom, use:
    lat = item.getElementsByTagName('lat')[0]
    lat.normalize()
    print lat.firstChild.data

Much easier when you can generate a proper binding from a schema, or use
something like Amara that does so without a schema.

Peter

On Tue, Apr 27, 2010 at 5:34 PM, Luis Miguel Morillas <morillas at gmail.com>wrote:

> 2010/4/27 kimmyaf <flahertyk1 at hotmail.com>:
> >
> > I don't really know... Here's the whole story.
> >
> > I am retrieving the xml by calling this link.
> >
> >
> http://maps.google.com/maps/api/geocode/xml?address=50+Oakland+St,Wellesley,MA,02481&sensor=true
> >
> >
> >
> > Here's the entire function:
> >
> > addr = '50+Oakland+St,Wellesley,MA,02481'
> >
> > def geocode_addr(addr):
> >    hostname =  'http://maps.google.com/maps/api/geocode/xml?'
> >    prefix = 'address='
> >    sensor = '&sensor=true'
> >    url = hostname + prefix + addr + sensor
> >
>
> I prefer amara:
>
> >>> from amara import bindery
> >>> doc = bindery.parse("
> http://maps.google.com/maps/api/geocode/xml?address=50+Oakland+St,Wellesley,MA,02481&sensor=true
> ")
> >>> locations = doc.xml_select(u'//location')
> >>> for loc in locations:
> ...     print loc.lat, loc.lng
> ...
> 42.3118520 -71.2632680
>
> ;)
>
> --lm
>
> >    print url
> >
> >    handler = urllib2.urlopen(url)
> >
> >    xml_response = handler.read()
> >    print xml_response
> >    #dom = minidom.parseString(xml_response)
> >    handler.close()
> >
> >    tree = ET.parse("GeocodeResponse.xml")
> >    print 'here'
> >    for tag in tree.getiterator("location"):
> >        print 'here1'
> >        print tag.findtext("lat")
> >        tag.findtext("lng")
> >
> >
> > *** I actually just pasted the xml from the shell where i printed
> > xml_response and saved it into an xml file in my folder called
> > GeocodeResponse.xml to test this... before going through the work of
> saving
> > the xml into a file. I got the "here" but not the "here1"
> >
> > I'm attaching my actual file..
> >
> > Sorry! I appreciate the help! this is the last piece of functionality i
> need
> > to get working for my programming assignment!
> >
> >
> >
> >
> >
> >
> >
> >
> > Stefan Behnel-3 wrote:
> >>
> >> kimmyaf, 26.04.2010 23:14:
> >>> Stefan Behnel-3 wrote:
> >>>> 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")
> >>>
> >>> Thanks Stefan. I tried this but it's not getting into the for block for
> >>> some
> >>> reason.
> >>
> >> Maybe the document uses namespace declarations that you forgot to show
> us?
> >>
> >> Stefan
> >> _______________________________________________
> >> XML-SIG maillist  -  XML-SIG at python.org
> >> http://mail.python.org/mailman/listinfo/xml-sig
> >>
> >>
> > http://old.nabble.com/file/p28382321/GeocodeResponse.xmlGeocodeResponse.xml
> > http://old.nabble.com/file/p28382321/GeocodeResponse.xmlGeocodeResponse.xml
> > --
> > View this message in context:
> http://old.nabble.com/parsing-XML-with-minidom-tp28359328p28382321.html
> > Sent from the Python - xml-sig mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > XML-SIG maillist  -  XML-SIG at python.org
> > http://mail.python.org/mailman/listinfo/xml-sig
> >
> _______________________________________________
> XML-SIG maillist  -  XML-SIG at python.org
> http://mail.python.org/mailman/listinfo/xml-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/xml-sig/attachments/20100427/a68a1ca3/attachment.html>


More information about the XML-SIG mailing list