[issue8277] ElementTree won't parse comments

Amaury Forgeot d'Arc report at bugs.python.org
Thu Apr 1 11:01:51 CEST 2010

Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

ElementTree does parse comments, it just omit them in the tree.
A quick search lead me to this page: http://effbot.org/zone/element-pi.htm
which can be further simplified:

from xml.etree import ElementTree
class MyTreeBuilder(ElementTree.TreeBuilder):
   def comment(self, data):
       self.start(ElementTree.Comment, {})
with open('c:/temp/t.xml', 'r') as f:
   xml = ElementTree.parse(
       f, parser=ElementTree.XMLParser(target=MyTreeBuilder()))

Now, should ElementTree do this by default? It's not certain, see how effbot's sample needs to wrap the entire file into another 'document' element.

nosy: +amaury.forgeotdarc, effbot

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list