[XML-SIG] Re: cElementTree limitations

Fredrik Lundh fredrik at pythonware.com
Fri Mar 11 13:26:09 CET 2005

Olivier Collioud wrote:

> what are cElementTree limitations compared to ElementTree ?

cElementTree implements the interfaces described here:


Comment support may be added in a future release.  In the meantime,
you can work around this limitation by creating a cElementTree node,
and setting the "tag" attribute to ElementTree.Comment:

    >>> import cElementTree
    >>> from elementtree.ElementTree import Comment
    >>> x = cElementTree.Element(Comment)
    >>> x.text = "hello"
    >>> cElementTree.tostring(x)
    '<!-- hello -->'

I recommend wrapping this functionality in a helper function that looks
for cElementTree.Comment, and only uses the workaround if necessary:

    def makecomment(text):
            elem = cElementTree.Comment(text)
        except AttributeError:
            elem = cElementTree.Element(ElementTree.Comment)
            elem.text = text
        return elem


More information about the XML-SIG mailing list