Java tutorial
//package com.java2s; import java.io.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { public static String documentToString(Document doc) throws TransformerConfigurationException, TransformerException, IOException { //return doc.getDocumentElement().toString(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); DOMSource src = new DOMSource(doc); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { StreamResult sr = new StreamResult(baos); transformer.transform(src, sr); String result = baos.toString(); return result; } finally { baos.close(); } } }