Here you can find the source of saveToXml(Document doc, OutputStream os)
public static void saveToXml(Document doc, OutputStream os) throws DOMException, TransformerException
//package com.java2s; 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.DOMException; import org.w3c.dom.Document; public class Main { public static void saveToXml(Document doc, OutputStream os) throws DOMException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); // factory.setAttribute("indent-number", new Integer(2)); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(os); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }/* w w w . jav a 2 s . com*/ }