This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author poke
Recipients amaury.forgeotdarc, effbot, flox, poke
Date 2010-04-01.17:24:05
SpamBayes Score 7.1417595e-08
Marked as misclassified No
Message-id <1270142647.82.0.567664076091.issue8277@psf.upfronthosting.co.za>
In-reply-to
Content
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 )
History
Date User Action Args
2010-04-01 17:24:07pokesetrecipients: + poke, effbot, amaury.forgeotdarc, flox
2010-04-01 17:24:07pokesetmessageid: <1270142647.82.0.567664076091.issue8277@psf.upfronthosting.co.za>
2010-04-01 17:24:05pokelinkissue8277 messages
2010-04-01 17:24:05pokecreate