title: Converting Document object to Byte[]
body: <p>I am init Document object like this:</p>
  
  <pre><code>DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document doc = docBuilder.newDocument();
  </code></pre>
  
  <p>After that I am building an XML file by inserting data to the doc object.</p>
  
  <p>Finally I am writing the contents to a file on my computer. </p>
  
  <p>My question is how to write the contents of doc in to a <code>byte[]</code>?*</p>
  
  <p>This is how i write the content to the XML file:</p>
  
  <pre><code>TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(doc);
  StreamResult result = new StreamResult(new File("changeOut.xml"));
  // Output to console for testing
  // StreamResult result = new StreamResult(System.out);
  transformer.transform(source, result);
  </code></pre>