libxmlop -- A libxml SAX driver for PyXML

Contents:

Back to top

What Is libxmlop?

libxmlop is a SAX2 driver for PyXML. It is implemented on top of the libxml XML parser.

libxmlop exposes the standard Python SAX2 interface and callbacks plus a few additional capabilities, which are described below.

You can learn more about libxml at http://xmlsoft.org/.

Back to top

Installation

See the file README.libxmlop. Basically, you will need to do the following:

  1. Download and build and install libxml2. You can find it http://www.xmlsoft.org.

    Note that on my machine I had to add the directory containing the installed libraries to the environment variable LD_LIBRARY_PATH. For example:

        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
    
  2. Change directory to the PyXML directory and unzip the libxmlop distribution. Something like the following:

    NOTE! The following replaces setup.py. If you have modified that file or have a version other than PyXML-0.7, you may want to patch in the libxmlop stanza instead.

        cd /Python/PyXML-0.7
        unzip ../libxmlop.zip
    
  3. Build and install libxmlop with the regular Distutils commands:
        python setup.py build
        python setup.py install
    
Back to top

Calling and Using libxmlop

Use that standard Python SAX2 interface.

Back to top

Extensions

libxmlop provides the following functions not provided by the standard SAX2 interface.

activateEventHandler

Turn a SAX2 callback off or on. After turning a call-back off, the content handler will not receive events for that call-back.

Prototype:

    parser.activateEventHandler(callback_name, flag)

Where:

Example:

    import xml.sax

    class ContentHandler:
        def __init__(self, parser):
            self.parser = parser
            self.target = ''
        def startDocument(self):
            self.parser.activateEventHandler('characters', 0)
        def startElement(self, name, attrs):
            if name == 'target':
                self.parser.activateEventHandler('characters', 1)
        def endElement(self, name, attrs):
            if name == 'target':
                self.parser.activateEventHandler('characters', 0)
        def characters(self, data):
            self.target += data

    def test(inFileName):
        parser = xml.sax.make_parser('xml.sax.drivers2.drv_libxmlop')
        handler = ContentHandler(parser)
        parser.setContentHandler(handler)
        parser.parse(inFileName)
Back to top

Additional Information

You can find more information about libxml at http://xmlsoft.org.

More information about PyXML is at http://www.python.org/sigs/xml-sig/.

Back to top

Last update: 4/11/02

Dave Kuhlman
[email protected]