Search code examples
xmlpython-2.7xsdlxmlpyxb

How to make a binding instance in lxml?


I've been using PyXB to generate bindings for xml flows using its parser, which works, but the parser is giving gives me performance issues. I would therefore would like to try and use lxml instead with my limited experience. How can I make my example here:

import pyxb.binding.saxer
import StringIO

saxer = pyxb.binding.saxer.make_parser()
handler = saxer.getContentHandler()
saxer.parse(StringIO.StringIO(xml))
instance = handler.rootObject()

In lxml?


Solution

  • There are examples of using different parsers including two flavors of lxml in pyxb/utils/saxutils.py, but these don't actually build bindings. examples/tmsxtvd can be used to evaluate performance of different parsers. Adding the following to dumpsample.py in that area seems to work, though the performance is not too different from the other methods: the bulk of the time is in processing the content to validate it.

    import lxml.sax
    import lxml.etree
    
    lsh = pyxb.binding.saxer.PyXBSAXHandler()
    lst1 = time.time()
    tree = lxml.etree.fromstring(xmld)
    lst2 = time.time()
    lxml.sax.saxify(tree, lsh)
    lst3 = time.time()
    lxml_instance = handler.rootObject()
    print('LXML-based read %f, parse and bind %f, total %f' % (lst2-lst1, lst3-lst2, lst3-lst1))
    print("Equality test on DOM vs LXML: %s" % (dom_instance.equal(lxml_instance),))