Validating cElementTrees with lxml

Stefan Behnel stefan_ml at
Sun Feb 17 07:54:36 CET 2008


Ryan K wrote:
> If I have a cElementTree.ElementTree (or the one from the Standard
> Library), can I use lxml's validation features on it since it
> implements the same ElementTree API?

Not directly. lxml and cElementTree use different tree models internally, so
you can't just apply C-implemented features of one to the other.

Any reason you can't just use lxml /instead/ of cElementTree?

In case you really want to combine both: to validate the tree, you only need
to create an lxml tree from your ElementTree in a one-way operation, which is
easy, as this can be done through the (identical) API. Just create a new lxml
Element and then traverse the ElementTree recursively, create a new SubElement
for each child you find, and set its text, tail and attrib properties.

Something like this might work (though untested):

    class TreeMigrator(object):
        def __init__(ET_impl_from, ET_impl_to):
            self.Element = ET_impl_to.Element
            self.SubElement = ET_impl_to.SubElement

        def copyChildren(self, from_parent, to_parent):
            for from_child in from_parent:
                tag = from_child.tag
                if not isinstance(tag, basestring): # skip Comments etc.
                to_child = self.SubElement(
                    to_parent, tag, **from_child.attrib)
                to_child.text = child.text
                to_child.tail = child.tail
                self.copyChildren(from_child, to_child)

        def __call__(self, from_root):
            tag = from_root.tag
            to_root = self.Element(tag, **from_root.attrib)
            to_root.text = from_root.text
            to_root.tail = from_root.tail
            if isinstance(tag, basestring): # skip Comments etc.
                self.copyChildren(from_root, to_root)
            return to_root

Feel free to finish it up and send it back to the list. :)


More information about the Python-list mailing list