Here you can find the source of documentToString(Node document)
public static String documentToString(Node document)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Node; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; public class Main { public static String documentToString(Node document) { try {/*from ww w . j a v a 2 s . c o m*/ StringWriter sw = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString().replace("\r\n", "\n"); } catch (TransformerException e) { throw new IllegalAccessError("Couldn't transform document to string"); } } }