title: Can ElementTree be told to preserve the order of attributes?
body: <p>I've written a fairly simple filter in python using ElementTree to munge the contexts of some xml files. And it works, more or less. </p>
  
  <p>But it reorders the attributes of various tags, and I'd like it to not do that.</p>
  
  <p>Does anyone know a switch I can throw to make it keep them in specified order?</p>
  
  <h2>Context for this</h2>
  
  <p>I'm working with and on a particle physics tool that has a complex, but oddly limited configuration system based on xml files. Among the many things setup that way are the paths to various static data files. These paths are hardcoded into the existing xml and there are no facilities for setting or varying them based on environment variables, and in our local installation they are necessarily in a different place.</p>
  
  <p>This isn't a disaster because the combined source- and build-control tool we're using allows us to shadow certain files with local copies. But even thought the data fields are static the xml isn't, so I've written a script for fixing the paths, but with the attribute rearrangement diffs between the local and master versions are harder to read than necessary.</p>
  
  <hr>
  
  <p>This is my first time taking ElementTree for a spin (and only my fifth or sixth python project) so maybe I'm just doing it wrong.</p>
  
  <p>Abstracted for simplicity the code looks like this:</p>
  
  <pre><code>tree = elementtree.ElementTree.parse(inputfile)
  i = tree.getiterator()
  for e in i:
      e.text = filter(e.text)
  tree.write(outputfile)
  </code></pre>
  
  <p>Reasonable or dumb?</p>
  
  <hr>
  
  <p>Related links:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/998514/how-can-i-get-the-order-of-an-element-attribute-list-using-python-xml-sax">How can I get the order of an element attribute list using Python xml.sax?</a></li>
  <li><a href="https://stackoverflow.com/questions/662624/preserve-order-of-attributes-when-modifying-with-minidom">Preserve order of attributes when modifying with minidom</a></li>
  </ul>