Here you can find the source of toWriter(Document doc, Writer writer)
public static void toWriter(Document doc, Writer writer)
//package com.java2s; /*/*from w w w . j a va 2 s .c om*/ * JFox - The most lightweight Java EE Application Server! * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn. * * JFox is licenced and re-distributable under GNU LGPL. */ import java.io.Writer; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; 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 { private final static TransformerFactory tf = TransformerFactory.newInstance(); public static void toWriter(Document doc, Writer writer) { if (doc == null || writer == null) { return; } try { Transformer tran = tf.newTransformer(); tran.setOutputProperty(OutputKeys.INDENT, "yes"); Source src = new DOMSource(doc); Result res = new StreamResult(writer); tran.transform(src, res); } catch (Exception e) { e.printStackTrace(); } } }