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):

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("";)):
    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.


Zope3-dev mailing list

Reply via email to