title: How to stream XML data using XOM?
body: <p>Say I want to output a huge set of search results, as XML, into a PrintWriter or an OutputStream, using <a href="http://xom.nu/" rel="nofollow noreferrer">XOM</a>. The resulting XML would look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;resultset&gt;
      &lt;result&gt;
         [child elements and data]
      &lt;/result&gt;
      ...
      ...
      [1000s of result elements more]
  &lt;/resultset&gt;
  </code></pre>
  
  <p>Because the resulting XML document could be big (hundreds of megabytes, perhaps), I want to output it in a streaming fashion (instead of creating the whole Document in memory and then writing that).</p>
  
  <p>The granularity of outputting one <code>&lt;result&gt;</code> at a time is fine, so I want to generate one <code>&lt;result&gt;</code> after another, and write it into the stream. In other words, I'd simply like to do something like this pseudocode (automatic flushing enabled, so don't worry about that):</p>
  
  <pre><code>open stream/writer
  write declaration
  write start tag for &lt;resultset&gt;
  while more results:
      write next &lt;result&gt; element
  write end tag for &lt;resultset&gt; 
  close stream/writer
  </code></pre>
  
  <p>I've been looking at <a href="http://xom.nu/apidocs/nu/xom/Serializer.html" rel="nofollow noreferrer"><code>Serializer</code></a>, but the necessary methods, <code>writeStartTag(Element)</code>, <code>writeEndTag(Element)</code>, <code>write(DocType)</code> are protected, not public! Is there no other way than to subclass Serializer to be able to use those methods, or to manually write the start and end tags directly into the stream as Strings, bypassing XOM altogether? (The latter wouldn't be too bad in this simple example, but in the general case it would get quite ugly.) </p>
  
  <p><strong>Am I missing something or is XOM just not made for this?</strong> </p>
  
  <p>With <a href="http://dom4j.sourceforge.net" rel="nofollow noreferrer">dom4j</a> I could do this easily using <a href="http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/io/XMLWriter.html" rel="nofollow noreferrer"><code>XMLWriter</code></a> - it has constructors that take a <code>Writer</code> or <code>OutputStream</code>, and methods <code>writeOpen(Element)</code>, <code>writeClose(Element)</code>, <code>writeDocType(DocumentType)</code> etc. Compare to XOM's <code>Serializer</code> where the only public <code>write</code> method is the one that takes a whole <code>Document</code>.</p>
  
  <p>(This is related to <a href="https://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j">my question about the best dom4j replacement</a> where XOM is a strong contender.)</p>