XML parsing with python

Stefan Behnel stefan_ml at behnel.de
Tue Aug 18 02:32:59 EDT 2009


John Posner wrote:
>> Use the iterparse() function of the xml.etree.ElementTree package.
> 
> iterparse() is too big a hammer for this purpose, IMO. How about this:
> 
>  from xml.etree.ElementTree import ElementTree
>  tree = ElementTree(None, "myfile.xml")
>  for elem in tree.findall('//book/title'):
>      print elem.text

Is that really so much better than an iterparse() version?

  from xml.etree.ElementTree import ElementTree

  for _, elem in ElementTree.iterparse("myfile.xml"):
      if elem.tag == 'book':
          print elem.findtext('title')
          elem.clear()

Stefan



More information about the Python-list mailing list