Patrick W. <p...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue8277>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to