title: Custom Java XMLBuilder vs Standard classes-based
body: <p>What is the best performance solution for XML generation.</p>
  
  <p>My goal is to build a few simple XMLs from code. I am going to implement simple custom StringBuffer based implementation of XML Builder. From other side there are several libraries like <a href="http://code.google.com/p/java-xmlbuilder/" rel="nofollow">http://code.google.com/p/java-xmlbuilder/</a> and <a href="http://code.google.com/p/xmltool/" rel="nofollow">http://code.google.com/p/xmltool/</a> which has nice DSL but I guess lack on performance.</p>
  
  <p>Since my goal is build simple enough XMLBuilder with great performance I think I will build custom solution. It will featuring:</p>
  
  <ul>
  <li>Nice Java-based DSL for XML constructs (adding tags basically)</li>
  <li>Great StringBuffer based performance.</li>
  <li>String data escape handling when adding XML tags.</li>
  <li>Auto-indent</li>
  </ul>
  
  <p>Please suggest if I am wrong on performance expectations and its probably better to use ready-made libraries.</p>
  
  <p><strong>UPDATE.</strong> Why I think the performance of standard xml builders is not very good.</p>
  
  <p>Standard XML builders uses Document Builder Factory and works with classes behind the scenes. Also these classes optimized to fit all users. For example I don't need namespace support etc.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"&gt;
  &lt;root&gt;
   &lt;testdata&gt;value&lt;/testdata&gt;
  &lt;/root&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>Consider very simple XML code above. If you build with standard tools it will involve so many work just to make this simple XML. I consider that it's better to just generate it by myself using String.</p>
  
  <p><strong>UPDATE 2</strong>. Performance requirement is that code should do as many things as required to generate simple XML and not more. </p>
  
  <p><strong>UPDATE 3. Thanks</strong> everyone for great comments! Now I understand better what I need and that my initial goal was not set very correctly with word "performance". My true goal is to use simple enough solution with convenient DSL to describe the XML structure and generate the XML output.</p>
  
  <p>I will use plain Java objects as DSL for XML and generate XML using XStream library which is pretty straightforward solution.</p>
  
  <p><strong>UPDATE 4. JAXB.</strong> I discussed XStream vs JAXB and found that JAXB is faster than XStream. Plus I already use JAXB in my project and I like its standard annotations. I change my mind and will go with JAXB for now because XStream was originally heavily developed at the time when JAXB was not so good as today.</p>