DOM to SAX using generators?

Martin v. Löwis loewis at informatik.hu-berlin.de
Mon Oct 14 11:55:41 EDT 2002


Alan Kennedy <alanmk at hotmail.com> writes:

> However, I've been itching to make use of generators, and at first this
> appeared to be a classic case for using generators. 

It doesn't appear to me that way. SAX events are no real objects, so
they don't need to be returned. Instead, a SAX event manifests itself
as a function call - which can readily be performed during traversal.

> def genEvents(node, consumer):
>     if node.nodeType == DOCUMENT_NODE:
>         consumer.startDocument()
>         # What goes here?
>         consumer.endDocument()

Between startDocument and endDocument, traversal of all child nodes
happens. So you recurse into the child nodes.

> How can I make this work? The obvious way that I can see to generate the
> events for the child nodes (in the section marked "What goes here") is
> to recursively visit the children. But that's the wrong approach, since
> I'll end up processing the children twice: once through the recursive
> descent and once through the series of nodes returned by the generator.

Right. Just drop the generator idea.

> How can I make generators to do this in a way that is more efficient
> than the simple recursive descent approach?

I can't imagine the being more efficient than a recursive descent;
that's already quite efficient.

Regards,
Martin



More information about the Python-list mailing list