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 )
#示例:
xml = ElementTree.parse( r'c: est.xml', parser = CommentedTreeBuilder() )
ElementTree.dump( xml )
#如果不加这个Handler,则读取和保存时注释会自动去掉(stripped out)