Here you can find the source of prettyPrint(final Document aNode)
public static final String prettyPrint(final Document aNode) throws Exception
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import org.w3c.dom.DOMConfiguration; import org.w3c.dom.Document; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; public class Main { public static final String prettyPrint(final Document aNode) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS"); // Prepare the output LSOutput domOutput = impls.createLSOutput(); domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name()); StringWriter writer = new StringWriter(); domOutput.setCharacterStream(writer); LSSerializer domWriter = impls.createLSSerializer(); DOMConfiguration domConfig = domWriter.getDomConfig(); domConfig.setParameter("format-pretty-print", true); domConfig.setParameter("element-content-whitespace", true); domWriter.setNewLine("\r\n"); domConfig.setParameter("cdata-sections", Boolean.TRUE); // And finaly, write domWriter.write(aNode, domOutput); return domOutput.getCharacterStream().toString(); }//from ww w .j a v a 2 s. c o m }