[Zope3-dev] Re: xml import / export in z2 & z3

Olivier Grisel ogrisel at nuxeo.com
Thu Dec 8 17:12:56 EST 2005


Jean-Marc Orliaguet a écrit :

> I've done some work with ElementTree for CPSIO, and I haven't found it 
> very easy to use because of all the extra namespace URI, and xpath stuff 
> used for the tree navigation (xpath_findall, ..) which seem to get in 
> the way. Also it could be that I find the DOM approach easier since I'm 
> used to it in javascript already.

The iterparse approach is by far the mots pythonic approach ever (tm):
http://effbot.org/zone/element-iterparse.htm

Quoting the URL, here is the RSS-reader-in-less-than-eight-lines example 
which is quite expressive:

"""
from urllib import urlopen
from cElementTree import iterparse

for event, elem in iterparse(urlopen("http://online.effbot.org/rss.xml")):
     if elem.tag == "item":
         print elem.findtext("link"), "-", elem.findtext("title")
         elem.clear() # won't need the children any more
"""

This combines the simplicity of the ElementTree data structure with the 
possibility to stream-process your input in similar way to SAX, cleaning 
the memory as you go.

-- 
Olivier



More information about the Zope3-dev mailing list