Here you can find the source of xmlDocumentToString(Document doc)
public static String xmlDocumentToString(Document doc)
//package com.java2s; import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 { public static String xmlDocumentToString(Document doc) { if (doc == null) { return ""; }/*from w w w. ja v a2s . c o m*/ String xmlDocString = ""; try { StringWriter writer = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(writer); transformer.transform(source, result); StringBuffer buffer = writer.getBuffer(); xmlDocString = buffer.toString(); } catch (TransformerConfigurationException e) { System.err.println("Unable to configure XML transformer"); e.printStackTrace(); } catch (TransformerException e) { System.err.println("Unable to transform XML"); e.printStackTrace(); } return xmlDocString; } }