title: Faithfully Preserve Comments in Parsed XML
body: <p>I'd like to preserve comments as faithfully as possible while manipulating XML.</p>
  
  <p>I managed to preserve comments, but the contents are getting XML-escaped.</p>
  
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python
  # add_host_to_tomcat.py
  
  import xml.etree.ElementTree as ET
  from CommentedTreeBuilder import CommentedTreeBuilder
  parser = CommentedTreeBuilder()
  
  if __name__ == '__main__':
      filename = "/opt/lucee/tomcat/conf/server.xml"
  
      # this is the important part: use the comment-preserving parser
      tree = ET.parse(filename, parser)
  
      # get the node to add a child to
      engine_node = tree.find("./Service/Engine")
  
      # add a node: Engine.Host
      host_node = ET.SubElement(
          engine_node,
          "Host",
          name="local.mysite.com",
          appBase="webapps"
      )
      # add a child to new node: Engine.Host.Context
      ET.SubElement(
          host_node,
          'Context',
          path="",
          docBase="/path/to/doc/base"
      )
  
      tree.write('out.xml')
  </code></pre>
  
  <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python
  # CommentedTreeBuilder.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 )
  </code></pre>
  
  <p>However, comments like like:</p>
  
  <pre class="lang-xml prettyprint-override"><code>  &lt;!--
  EXAMPLE HOST ENTRY:
      &lt;Host name="lucee.org" appBase="webapps"&gt;
           &lt;Context path="" docBase="/var/sites/getrailo.org" /&gt;
       &lt;Alias&gt;www.lucee.org&lt;/Alias&gt;
       &lt;Alias&gt;my.lucee.org&lt;/Alias&gt;
      &lt;/Host&gt;
  
  HOST ENTRY TEMPLATE:
      &lt;Host name="[ENTER DOMAIN NAME]" appBase="webapps"&gt;
           &lt;Context path="" docBase="[ENTER SYSTEM PATH]" /&gt;
       &lt;Alias&gt;[ENTER DOMAIN ALIAS]&lt;/Alias&gt;
      &lt;/Host&gt;
    --&gt;
  </code></pre>
  
  <p>End up as:</p>
  
  <pre class="lang-xml prettyprint-override"><code>  &lt;!--
              EXAMPLE HOST ENTRY:
      &amp;lt;Host name="lucee.org" appBase="webapps"&amp;gt;
           &amp;lt;Context path="" docBase="/var/sites/getrailo.org" /&amp;gt;
           &amp;lt;Alias&amp;gt;www.lucee.org&amp;lt;/Alias&amp;gt;
           &amp;lt;Alias&amp;gt;my.lucee.org&amp;lt;/Alias&amp;gt;
      &amp;lt;/Host&amp;gt;
  
      HOST ENTRY TEMPLATE:
      &amp;lt;Host name="[ENTER DOMAIN NAME]" appBase="webapps"&amp;gt;
           &amp;lt;Context path="" docBase="[ENTER SYSTEM PATH]" /&amp;gt;
           &amp;lt;Alias&amp;gt;[ENTER DOMAIN ALIAS]&amp;lt;/Alias&amp;gt;
      &amp;lt;/Host&amp;gt;
     --&gt;
  </code></pre>
  
  <p>I also tried <code>self._target.data( saxutils.unescape(data) )</code> in <code>CommentedTreeBuilder.py</code>, but it didn't seem to do anything. In fact, I think the problem happens somewhere after the <code>handle_commment()</code> step.</p>
  
  <p>By the way, this question is similar to <a href="https://stackoverflow.com/questions/4474754/how-to-keep-comments-while-parsing-xml-using-python-elementtree">this</a>.</p>