Action against software patentsGnome2 LogoW3C LogoRed Hat Logo
Made with Libxml2 Logo

The XML C parser and toolkit of Gnome

The SAX interface

Developer Menu
API Indexes
Related links

Sometimes the DOM tree output is just too large to fit reasonablyintomemory. In that case (and if you don't expect to save back the XMLdocumentloaded using libxml), it's better to use the SAX interface of libxml.SAX isa callback-based interfaceto the parser. Beforeparsing,the application layer registers a customized set of callbacks whicharecalled by the library as it progresses through the XML input.

To get more detailed step-by-step guidance on using the SAX interfaceoflibxml, see the nicedocumentation.writtenby JamesHenstridge.

You can debug the SAX behaviour by using thetestSAXprogram located in the gnome-xml module (it's usuallynot shipped in thebinary packages of libxml, but you can find it in the tarsourcedistribution). Here is the sequence of callbacks that would be reportedbytestSAX when parsing the example XML document shown earlier:

SAX.setDocumentLocator()
SAX.startDocument()
SAX.getEntity(amp)
SAX.startElement(EXAMPLE, prop1='gnome is great', prop2='& linux too')
SAX.characters(   , 3)
SAX.startElement(head)
SAX.characters(    , 4)
SAX.startElement(title)
SAX.characters(Welcome to Gnome, 16)
SAX.endElement(title)
SAX.characters(   , 3)
SAX.endElement(head)
SAX.characters(   , 3)
SAX.startElement(chapter)
SAX.characters(    , 4)
SAX.startElement(title)
SAX.characters(The Linux adventure, 19)
SAX.endElement(title)
SAX.characters(    , 4)
SAX.startElement(p)
SAX.characters(bla bla bla ..., 15)
SAX.endElement(p)
SAX.characters(    , 4)
SAX.startElement(image, href='linus.gif')
SAX.endElement(image)
SAX.characters(    , 4)
SAX.startElement(p)
SAX.characters(..., 3)
SAX.endElement(p)
SAX.characters(   , 3)
SAX.endElement(chapter)
SAX.characters( , 1)
SAX.endElement(EXAMPLE)
SAX.endDocument()

Most of the other interfaces of libxml2 are based on the DOMtree-buildingfacility, so nearly everything up to the end of this documentpresupposes theuse of the standard DOM tree build. Note that the DOM treeitself is built bya set of registered default callbacks, without internalspecificinterface.

Daniel Veillard