Re: parsing XML with minidom

Stefan Behnel <[email protected]> Wed, 28 Apr 2010 07:43:32 +0200
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
kimmyaf, 27.04.2010 23:32:
>>      handler = urllib2.urlopen(url)
>>      xml_response = handler.read()
>>      handler.close()
>>
>>      tree = ET.parse("GeocodeResponse.xml")

 >> Do I have to use a file? I tried to do
 >>
 >> tree = ET.parse(xml_response)

parse() is meant for parsing files. Use fromstring() to parse from a string.

This works for me:

   >>> import xml.etree.cElementTree as ET
   >>> tree = ET.parse('gmap.xml')
   >>> print [ (el.findtext('lat'), el.findtext('lng'))
   ...         for el in tree.getiterator('location') ]
   [('42.3118520', '-71.2632680')]

Stefan
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig