title: How to keep comments while parsing XML using Python / ElementTree
body: <p><strong>Currently using Python 2.4.3, and not allowed to upgrade</strong></p>
  
  <p><em>I want to change the values of a given attribute in one or more tags, together with  XML-comments in the updated file.</em></p>
  
  <p>I have managed to create a Python script that takes a XML-file as argument, and for each tag specified changes an attribute, as shown below</p>
  
  <pre><code>def update(file, state):
      global Etree
      try:
          from elementtree import ElementTree
          print '*** using ElementTree'
      except ImportError, e:
          print '***'
          print '*** Error: Must install either ElementTree or lxml.'
          print '***'
          raise ImportError, 'must install either ElementTree or lxml'
      #end try
  
      doc = Etree.parse(file)
      root = doc.getroot()
  
      for element in root.findall('.//StateManageable'):
          element.attrib['initialState'] = state
      #end for
      doc.write(file)
  #end def
  </code></pre>
  
  <p>This is all fine, the attributes "initialState" are updated, except for the fact that my original XML contains a lot of XML comments as well, but they are long gone, which is bad. </p>
  
  <p>Suspect that parse only retrieves the XML-structure, but I thought XML-comments where a part of the structure. I also realize that the "human-readable" formatting of my original document is long gone, but that I have realized is expected behavior, need to format afterwards using <code>xmllint --format</code> or XSL.</p>