Here you can find the source of domToString(final Document document)
private static String domToString(final Document document) throws Exception
//package com.java2s; //License from project: BSD License import java.io.StringWriter; import javax.xml.transform.Transformer; 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 domToString(final Document document) throws Exception { String result = null;/* w ww. ja va 2s .c o m*/ StringWriter strWtr = new StringWriter(); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); if (document != null) { StreamResult strResult = new StreamResult(strWtr); transformer.transform(new DOMSource(document.getDocumentElement()), strResult); result = strResult.getWriter().toString(); } return result; } }