[issue8277] ElementTree won't parse comments

Patrick W. report at bugs.python.org
Thu Apr 1 19:24:07 CEST 2010


Patrick W. <poke at BornToLaugh.de> added the comment:

Thanks for your reply, Amaury. That page really might mean that it was not intended for ElementTree to parse such things by default. Although it might be nice if there was some easy way to simply enable it, instead of having to hack it into there and depending on details of some internal code (which might change in the future).

Your code btw. didn't work for me, but based on it and on that effbot page, I came up with the following solution, which works fine.

test.py
-------
from xml.etree import ElementTree

class CommentedTreeBuilder ( ElementTree.XMLTreeBuilder ):
    def __init__ ( self, html = 0, target = None ):
        ElementTree.XMLTreeBuilder.__init__( self, html, target )
        self._parser.CommentHandler = self.handle_comment
    
    def handle_comment ( self, data ):
        self._target.start( ElementTree.Comment, {} )
        self._target.data( data )
        self._target.end( ElementTree.Comment )


with open( 'test.xml', 'r' ) as f:
    xml = ElementTree.parse( f, parser = CommentedTreeBuilder() )
ElementTree.dump( xml )

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8277>
_______________________________________


More information about the Python-bugs-list mailing list