code: static void serialize(final Document doc, final OutputStream os, final String encoding)
          throws TransformerFactoryConfigurationError, TransformerException, IOException
    {
      if (doc == null)
        throw new IllegalArgumentException("No document provided.");
  
      if (os == null)
        throw new IllegalArgumentException("No output stream provided");
  
      if (encoding == null || encoding.isEmpty())
        throw new IllegalArgumentException("No encoding provided.");
  
      final TransformerFactory transformerFactory = TransformerFactory.newInstance();
      transformerFactory.setAttribute("indent-number", Integer.valueOf(2));
      final Transformer t = transformerFactory.newTransformer();
  
      t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
      t.setOutputProperty(OutputKeys.METHOD, "xml");
      t.setOutputProperty(OutputKeys.INDENT, "yes");
      t.setOutputProperty(OutputKeys.ENCODING, encoding);
  
      final OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
      t.transform(new DOMSource(doc), new StreamResult(osw));
      osw.flush();
    }
docs: Serializes a DOM. The OutputStream handed over to this method is not closed inside this method.