Here you can find the source of writeDocument(Document doc, OutputStream os)
public static OutputStream writeDocument(Document doc, OutputStream os)
//package com.java2s; //License from project: Apache License import java.io.OutputStream; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 OutputStream writeDocument(Document doc, OutputStream os) { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer;//from w w w . j a va 2s.c o m try { transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(os); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return os; } }