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

_______________________________________________
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to