Here you can find the source of document2String(Document doc, boolean prettyPrint)
public static String document2String(Document doc, boolean prettyPrint) throws Exception
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; 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 { public static String document2String(Document doc, boolean prettyPrint) throws Exception { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance() .newTransformer();/*from w w w. j ava 2 s . c om*/ if (prettyPrint) { transformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); } }