Passing generated trees through Python

This is the most simple way to integrate with lxml. It does not require
any C-level integration but uses a Python function to wrap an externally
generated libxml2 document in lxml.

The external module that creates the libxml2 tree must pack the document
pointer into a PyCapsule
object. This can then be passed into lxml with the function
lxml.etree.adopt_external_document(). It also takes an optional lxml
parser instance to associate with the document, in order to configure the
Element class lookup, relative URL lookups, etc.

Writing external modules in C

If you really feel like it, you can also interface with lxml.etree straight
from C code. All you have to do is include the header file for the public
API, import the lxml.etree module and then call the import function: