Here you can find the source of writeOutDocument(OutputStream os, Document doc)
public static void writeOutDocument(OutputStream os, Document doc) throws TransformerException, IOException
//package com.java2s; import java.io.IOException; import java.io.OutputStream; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { public static void writeOutDocument(OutputStream os, Document doc) throws TransformerException, IOException { DOMSource source = new DOMSource(doc); StreamResult sResult = new StreamResult(os); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer t = tFactory.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "utf-8"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(source, sResult);/*from w w w . ja va2 s. co m*/ sResult.getOutputStream().close(); } }