[Tutor] xml parsing from xml

Stefan Behnel stefan_ml at behnel.de
Wed May 7 18:56:34 CEST 2014


Alan Gauld, 07.05.2014 18:11:
> Python comes with several XML parsers. The simplest to use are probably sax
> and ElementTree (aka etree). The documenation gives examples of both. sax
> is easiest and fastest for simple XML in big files while etree is probably
> better for more complex XML structures.

I wouldn't say that SAX qualifies as "easiest". Sure, if the task is
something like "count number of abc tags" or "find tag xyz and get an
attribute value from it", then SAX is relatively easy and also quite fast.
However, anything larger than that (i.e. *any* real task) quickly gets so
complex and complicated that it's usually faster to learn ElementTree's
iterparse() from scratch and write a working solution with it, than to
write even a half working implementation of a SAX handler for a given
problem. And that's completely ignoring the amount of time that such an
unwieldy SAX handler will cost you in terms of long term code maintenance.

BTW, ElementTree also has a SAX-like parsing mode, but comes with a simpler
interface and saner parser configuration defaults. That makes the xml.sax
package even less recommendable.

Stefan




More information about the Tutor mailing list