Thursday, March 31, 2011

Finding top-level xml comments using Python's ElementTree

I'm parsing an xml file using Python's ElementTree, like that:

et = ElementTree(file=file("test.xml"))

test.xml starts with a few lines of xml comments.

Is there a way to get those comments from et?

From stackoverflow
  • For ElementTree 1.2.X there is an article on Reading processing instructions and comments with ElementTree (http://effbot.org/zone/element-pi.htm).


    EDIT:

    The alternative would be using lxml.etree which implements the ElementTree API. A quote from ElementTree compatibility of lxml.etree :

    ElementTree ignores comments and processing instructions when parsing XML, while etree will read them in and treat them as Comment or ProcessingInstruction elements respectively.

    Paul Oyster : unfortunately, there is no CommentHandler in the version that ships with python 2.5
    Paul Oyster : Also, it is too risky to use internal library stuff, as the code should work with future versions of python.
    f3lix : Those are legitimate arguments. I edited my answer and added an alternative solution.

0 comments:

Post a Comment