title: How to save ElementTree as UTF-16
body: <p>I'm trying to save an XML file encoded as UTF-16 with cElementTree. This is the same project, but different than the DOCTYPE issue in: <a href="https://stackoverflow.com/questions/8868248/how-to-create-doctype-with-pythons-celementtree">How to create &lt;!DOCTYPE&gt; with Python&#39;s cElementTree</a></p>
  
  <p>I've learned that if I do not declare the encoding in the string, cElementTree will add it. So, the code is like this:</p>
  
  <pre><code>import xml.etree.cElementTree as ElementTree
  from StringIO import StringIO
  s = '&lt;?xml version=\"1.0\" ?&gt;&lt;!DOCTYPE tmx SYSTEM \"tmx14a.dtd\" &gt;&lt;tmx version=\"1.4a\" /&gt;'
  tree = ElementTree.parse(StringIO(s)).getroot()
  header = ElementTree.SubElement(tree,'header',{'adminlang': 'EN',})
  body = ElementTree.SubElement(tree,'body')
  ElementTree.ElementTree(tree).write('myfile.tmx','UTF-16')
  </code></pre>
  
  <p>When I write the file with UTF-8, everthing's great. However, when I change to UTF-16, the text encoding is corrupted. It is also missing the required Byte Order Marker. When I try adding the BOM to the start of the string,</p>
  
  <pre><code>s = '\xFF\xFE&lt;?xml version=\"1.0\"......
  </code></pre>
  
  <p>ElementTree reports the error "not well-formed (invalid token) line 1, column 1". </p>
  
  <p>All the buffers are unicode data. How can I save to a UTF-16 XML file?</p>