Here you can find the source of serialize(Document document, Writer writer)
static void serialize(Document document, Writer writer)
//package com.java2s; /*// www . j a v a 2s. co m * Copyright 2011 StackFrame, LLC * * This is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * You should have received a copy of the GNU General Public License * along with this file. If not, see <http://www.gnu.org/licenses/>. */ import java.io.Writer; import javax.xml.transform.OutputKeys; 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 { static void serialize(Document document, Writer writer) { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", new Integer(4)); try { Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(writer); transformer.transform(new DOMSource(document), result); } catch (Exception e) { throw new RuntimeException(e); } } }