Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; public class Main { private static DOMImplementationLS impl; /** * Serializes the given document into a string * * @param doc The document to serialize * @return The XML document as a string (pretty-printed) */ public static String documentToString(Document doc) { if (doc == null) return "null"; LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); return writer.writeToString(doc); } }