Here you can find the source of documentToString(Document document)
public static String documentToString(Document document)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.Document; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; public class Main { public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;/* w w w .j ava 2 s . c o m*/ try { transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } return writer.getBuffer().toString().replaceAll("\n|\r", ""); } }