Writing an application for a SAX-compliant XML parser

Simon Pepping

Published 19 January 1999

Revision History
Revision 0.327 December 1998
Initial publication
Revision 0.431 December 1998
Small change in how to specify a SAX driver file
Revision 0.519 January 1999
Several changes suggested by Lars M. Garshol

I give a short overview of the Simple API for XML (SAX). I describe how a SAX-compliant parser and a SAX application interact, and how one should proceed to write a SAX application. The description focuses on the Python implementation of SAX. The examples are written in Python.


Table of Contents
Summary
Scheme of the interaction between an application and an XML parser
Detailed write-up
Finding a parser, using the parser factory
An example

Summary

  1. Write DocumentHandler, DTDHandler, EntityResolver, and ErrorHandler classes.

  2. Select a SAX-compliant parser. E.g.,
        SAXparser=xml.sax.drivers.drv_xmlproc.SAX_XPParser().

  3. Register the handlers with the parser. E.g.,
    	SAXparser.setDocumentHandler(DocumentHandler())
        SAXparser.setErrorHandler(ErrorHandler())

  4. Run the parser by calling its parse method. E.g.,
        SAXparser.parse(sys.argv[1])

  5. Watch the result, which is completely determined by the handlers of your application.

This file was written according to the LinuxDoc97 dtd. It was converted to the DocBook 3.0 dtd and from there to the various presentations by SGMLTools 2.0.0.