Re: parsing XML with minidom

Luis Miguel Morillas <[email protected]> Wed, 28 Apr 2010 00:34:54 +0200
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
2010/4/27 kimmyaf <[email protected]>:
>
> 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=3D50+Oakland+St,Welle=
sley,MA,02481&sensor=3Dtrue
>
>
>
> Here's the entire function:
>
> addr =3D '50+Oakland+St,Wellesley,MA,02481'
>
> def geocode_addr(addr):
> =A0 =A0hostname =3D =A0'http://maps.google.com/maps/api/geocode/xml?'
> =A0 =A0prefix =3D 'address=3D'
> =A0 =A0sensor =3D '&sensor=3Dtrue'
> =A0 =A0url =3D hostname + prefix + addr + sensor
>

I prefer amara:

>>> from amara import bindery
>>> doc =3D bindery.parse("http://maps.google.com/maps/api/geocode/xml?addr=
ess=3D50+Oakland+St,Wellesley,MA,02481&sensor=3Dtrue")
>>> locations =3D doc.xml_select(u'//location')
>>> for loc in locations:
...     print loc.lat, loc.lng
...
42.3118520 -71.2632680

;)

--lm

> =A0 =A0print url
>
> =A0 =A0handler =3D urllib2.urlopen(url)
>
> =A0 =A0xml_response =3D handler.read()
> =A0 =A0print xml_response
> =A0 =A0#dom =3D minidom.parseString(xml_response)
> =A0 =A0handler.close()
>
> =A0 =A0tree =3D ET.parse("GeocodeResponse.xml")
> =A0 =A0print 'here'
> =A0 =A0for tag in tree.getiterator("location"):
> =A0 =A0 =A0 =A0print 'here1'
> =A0 =A0 =A0 =A0print tag.findtext("lat")
> =A0 =A0 =A0 =A0tag.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 savi=
ng
> 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 n=
eed
> 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 wh=
at
>>>> data you mean.
>>>>
>>>> Here is some untested code using xml.etree.cElementTree:
>>>>
>>>> =A0 =A0 =A0 import xml.etree.cElementTree as ET
>>>> =A0 =A0 =A0 tree =3D ET.parse("thefile.xml")
>>>> =A0 =A0 =A0 for tag in tree.getiterator("location"):
>>>> =A0 =A0 =A0 =A0 =A0 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 u=
s?
>>
>> Stefan
>> _______________________________________________
>> XML-SIG maillist =A0- [email protected]
>> http://mail.python.org/mailman/listinfo/xml-sig
>>
>>
> http://old.nabble.com/file/p28382321/GeocodeResponse.xml GeocodeResponse.=
xml
> http://old.nabble.com/file/p28382321/GeocodeResponse.xml GeocodeResponse.=
xml
> --
> View this message in context: http://old.nabble.com/parsing-XML-with-mini=
dom-tp28359328p28382321.html
> Sent from the Python - xml-sig mailing list archive at Nabble.com.
>
> _______________________________________________
> XML-SIG maillist =A0- [email protected]
> http://mail.python.org/mailman/listinfo/xml-sig
>
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig