title: Java XML library that preserves attribute order
body: <p>I am writing a Java program that reads an XML file, makes some modifications, and writes back the XML.</p>
  <p>Using the standard Java XML DOM API, the order of the attributes is not preserved.</p>
  <p>That is, if I have an input file such as:</p>
  <pre><code>&lt;person first_name=&quot;john&quot; last_name=&quot;lederrey&quot;/&gt;
  </code></pre>
  <p>I might get an output file as:</p>
  <pre><code>&lt;person last_name=&quot;lederrey&quot; first_name=&quot;john&quot;/&gt;
  </code></pre>
  <p>That's correct, because the XML specification says that order attribute is not significant.</p>
  <p>However, my program needs to preserve the order of the attributes, so that a person can easily compare the input and output document with a diff tool.</p>
  <p>One solution for that is to process the document with SAX (instead of DOM):
  <em><a href="https://stackoverflow.com/questions/726395/order-of-xml-attributes-after-dom-processing/3728241#3728241">Order of XML attributes after DOM processing</a></em></p>
  <p>However, this does not work for my case,
  because the transformation I need to do in one node might depend on a <a href="https://en.wikipedia.org/wiki/XPath" rel="nofollow noreferrer">XPath</a> expression on the whole document.</p>
  <p>So, the simplest thing would be to have a XML library very similar to the standard Java DOM library, with the exception that it preserves the attribute order.</p>
  <p>Is there such a library?</p>
  <p>PS: Please, avoid discussing whether I should the preserve attribute order or not. This is a very interesting discussion, but it is not the point of this question.</p>