Here you can find the source of documentToString(Document doc)
private static String documentToString(Document doc) throws TransformerException
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import javax.xml.transform.OutputKeys; 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 String documentToString(Document doc) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String xml = writer.getBuffer().toString().replaceAll("\n|\r", ""); return xml; }//from ww w . j a v a2 s . c o m }