Here you can find the source of documentToString(Document document)
public static String documentToString(Document document)
//package com.java2s; //License from project: Apache License import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; 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 static TransformerFactory tf; public static String documentToString(Document document) { try {/* www . j a va 2 s . c o m*/ Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString(); } catch (TransformerException tEx) { tEx.printStackTrace(); } return null; } public static String documentToString(Document document, boolean omitXmlDeclaration) { try { Transformer trans = tf.newTransformer(); trans.setOutputProperty("omit-xml-declaration", omitXmlDeclaration ? "yes" : "no"); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString(); } catch (TransformerException tEx) { tEx.printStackTrace(); } return null; } }