[XML-SIG] parsing XML with minidom

Luis Miguel Morillas morillas at gmail.com
Wed Apr 28 00:34:54 CEST 2010


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.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-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
>


More information about the XML-SIG mailing list