Gnome LogoW3C LogoRed Hat Logo

The XML C library for Gnome

Python and bindings

Main Menu
API Indexes
Related links

There are a number of language bindings and wrappers available for libxml2, the list below is not exhaustive. Please contact the (archives) in order to get updates to this list or to discuss the specific topic of libxml2 or libxslt wrappers or bindings:

The distribution includes a set of Python bindings, which are guaranteed to be maintained as part of the library in the future, though the Python interface have not yet reached the maturity of the C API.

To install the Python bindings there are 2 options:

  • If you use an RPM based distribution, simply install the libxml2-python RPM (and if needed the libxslt-python RPM).
  • Otherwise use the libxml2-python module distribution corresponding to your installed version of libxml2 and libxslt. Note that to install it you will need both libxml2 and libxslt installed and run "python build install" in the module tree.

The distribution includes a set of examples and regression tests for the python bindings in the python/tests directory. Here are some excerpts from those tests:

This is a basic test of the file interface and DOM navigation:

import libxml2

doc = libxml2.parseFile("tst.xml")
if != "tst.xml":
    print " failed"
root = doc.children
if != "doc":
    print " failed"
child = root.children
if != "foo":
    print " failed"

The Python module is called libxml2; parseFile is the equivalent of xmlParseFile (most of the bindings are automatically generated, and the xml prefix is removed and the casing convention are kept). All node seen at the binding level share the same subset of accessors:

  • name : returns the node name
  • type : returns a string indicating the node type
  • content : returns the content of the node, it is based on xmlNodeGetContent() and hence is recursive.
  • parent , children, last, next, prev, doc, properties: pointing to the associated element in the tree, those may return None in case no such link exists.

Also note the need to explicitly deallocate documents with freeDoc() . Reference counting for libxml2 trees would need quite a lot of work to function properly, and rather than risk memory leaks if not implemented correctly it sounds safer to have an explicit function to free a tree. The wrapper python objects like doc, root or child are them automatically garbage collected.

This test check the validation interfaces and redirection of error messages:

import libxml2

#deactivate error messages from the validation
def noerr(ctx, str):

libxml2.registerErrorHandler(noerr, None)

ctxt = libxml2.createFileParserCtxt("invalid.xml")
doc = ctxt.doc()
valid = ctxt.isValid()
if valid != 0:
    print "validity check failed"

The first thing to notice is the call to registerErrorHandler(), it defines a new error handler global to the library. It is used to avoid seeing the error messages when trying to validate the invalid document.

The main interest of that test is the creation of a parser context with createFileParserCtxt() and how the behaviour can be changed before calling parseDocument() . Similarly the informations resulting from the parsing phase are also available using context methods.

Contexts like nodes are defined as class and the libxml2 wrappers maps the C function interfaces in terms of objects method as much as possible. The best to get a complete view of what methods are supported is to look at the module containing all the wrappers.

This test show how to activate the push parser interface:

import libxml2

ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml")
ctxt.parseChunk("/>", 2, 1)
doc = ctxt.doc()


The context is created with a special call based on the xmlCreatePushParser() from the C library. The first argument is an optional SAX callback object, then the initial set of data, the length and the name of the resource in case URI-References need to be computed by the parser.

Then the data are pushed using the parseChunk() method, the last call setting the third argument terminate to 1.

this test show the use of the event based parsing interfaces. In this case the parser does not build a document, but provides callback information as the parser makes progresses analyzing the data being provided:

import libxml2
log = ""

class callback:
    def startDocument(self):
        global log
        log = log + "startDocument:"

    def endDocument(self):
        global log
        log = log + "endDocument:"

    def startElement(self, tag, attrs):
        global log
        log = log + "startElement %s %s:" % (tag, attrs)

    def endElement(self, tag):
        global log
        log = log + "endElement %s:" % (tag)

    def characters(self, data):
        global log
        log = log + "characters: %s:" % (data)

    def warning(self, msg):
        global log
        log = log + "warning: %s:" % (msg)

    def error(self, msg):
        global log
        log = log + "error: %s:" % (msg)

    def fatalError(self, msg):
        global log
        log = log + "fatalError: %s:" % (msg)

handler = callback()

ctxt = libxml2.createPushParser(handler, "<foo", 4, "test.xml")
chunk = " url='tst'>b"
ctxt.parseChunk(chunk, len(chunk), 0)
chunk = "ar</foo>"
ctxt.parseChunk(chunk, len(chunk), 1)

reference = "startDocument:startElement foo {'url': 'tst'}:" + \ 
            "characters: bar:endElement foo:endDocument:"
if log != reference:
    print "Error got: %s" % log
    print "Expected: %s" % reference

The key object in that test is the handler, it provides a number of entry points which can be called by the parser as it makes progresses to indicate the information set obtained. The full set of callback is larger than what the callback class in that specific example implements (see the SAX definition for a complete list). The wrapper will only call those supplied by the object when activated. The startElement receives the names of the element and a dictionary containing the attributes carried by this element.

Also note that the reference string generated from the callback shows a single character call even though the string "bar" is passed to the parser from 2 different call to parseChunk()

This is a basic test of XPath wrappers support

import libxml2

doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval("//*")
if len(res) != 2:
    print "xpath query: wrong node set size"
if res[0].name != "doc" or res[1].name != "foo":
    print "xpath query: wrong node set value"

This test parses a file, then create an XPath context to evaluate XPath expression on it. The xpathEval() method execute an XPath query and returns the result mapped in a Python way. String and numbers are natively converted, and node sets are returned as a tuple of libxml2 Python nodes wrappers. Like the document, the XPath context need to be freed explicitly, also not that the result of the XPath query may point back to the document tree and hence the document must be freed after the result of the query is used.

This test shows how to extend the XPath engine with functions written in python:

import libxml2

def foo(ctx, x):
    return x + 1

doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
res = ctxt.xpathEval("foo(1)")
if res != 2:
    print "xpath extension failure"

Note how the extension function is registered with the context (but that part is not yet finalized, this may change slightly in the future).

This test is similar to the previous one but shows how the extension function can access the XPath evaluation context:

def foo(ctx, x):
    global called

    # test that access to the XPath evaluation contexts
    pctxt = libxml2.xpathParserContext(_obj=ctx)
    ctxt = pctxt.context()
    called = ctxt.function()
    return x + 1

All the interfaces around the XPath parser(or rather evaluation) context are not finalized, but it should be sufficient to do contextual work at the evaluation point.

Memory debugging:

last but not least, all tests starts with the following prologue:

#memory debug specific

and ends with the following epilogue:

#memory debug specific
if libxml2.debugMemory(1) == 0:
    print "OK"
    print "Memory leak %d bytes" % (libxml2.debugMemory(1))

Those activate the memory debugging interface of libxml2 where all allocated block in the library are tracked. The prologue then cleans up the library state and checks that all allocated memory has been freed. If not it calls dumpMemory() which saves that list in a .memdump file.

Daniel Veillard