Python 2.0+ and PyXML provide SAX2 libraries for general use.
This slide and the following from Alexandre Fayolles' excellent EuroPython 2002 tutorial on Python/XML processing is an great introduction to SAX processing in Python.
Core documentation is part of Python:
13.9 xml.sax -- Support for SAX2 parsers
Unfortunately this does not include any examples. You can find some such in the following resources:
5 SAX: The Simple API for XML , from the Python/XML HOWTO by Andrew Kuchling
An important point made in this document and in not enough other places is in section 5.5:
"If namespace processing is active, parsers won't call startElement(), but instead will call a method named startElementNS(). The default of this setting varies from parser to parser, so you should always set it to a safe value (unless your handler supports both namespace-aware and -unaware processing)."
The converse is also true: If namespace processing is not active, parsers won't call startElementNS(), but instead will call a method named startElement().
Unfortunately, none of the examples in this document show namespace processing.
Charming Python: Revisiting XML tools for Python, by David Mertz has a SAX example.
Warning: he does not set the namespace feature. After
parser = xml.sax.make_parser()
I would add
parser.setFeature(xml.sax.handler.feature_namespaces, 1)
Jürgen Hermann's Python Cookbook recipe "Using the SAX2 LexicalHandler Interface" is an great example of SAX, and up to date.
Another cookbook recipe with a nice and detailed example of Python/SAX is "Colorize xml source using", by Sylvain Thenault
Paul Prescod's recipe "Extract text from XML document" is quite brief, but a useful routine in itself. Also see his "Count tags in a document".
Mike Hostetler's recipe Breaking large XML documents into chunks to speed processing uses SAX to create a very skeletal sort of pull DOM.
If you are parsing a document that uses the external subset of a DTD, check out this message where Sylvain Thénault points to SAX features for finer control.
See this thread for some discussion of how to create lightweight Python data structures from SAX and the raw xml.parsers.expat API (which is somewhat SAX-like).
If you don't mind swimming through ads, see "Parsing XML with SAX and Python" by Nadia Poulou.
Beware the following obsolete documents:
Writing an application for a SAX-compliant XML parser
Charming Python: Tinkering with XML and Python, by David Mertz. This is actually a good overall intro to the Python/XML world, but it dates from June 2000 and much has changed since then. See Charming Python: Revisiting XML tools for Python for updates.
XML Processing with Python, by Sean McGrath (December 06, 1999)
