3

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

Yes, there is a way.

from xml.etree import ElementTree

def parseXml (fn):
    try:
        target = ElementTree.TreeBuilder (insert_comments=True)
        parser = ElementTree.XMLParser(target=target, encoding='utf-8')
        et = ElementTree.parse (fn, parser)
    except Exception as e:
        print ("%s: %s" % (fn, str (e)))
        return

    for el in et.getroot ():
        print (el)

Same applies to processing instructions (ElementTree.PI), they will be inserted into the resulting structure only if insert_pis=true.

1 Comment

Note that this requires Python 3.8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.