Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.OutputStream; 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; import org.w3c.dom.Element; public class Main { public static String DocumentToString(Document doc) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ElementToStream(doc.getDocumentElement(), baos); return new String(baos.toByteArray()); } public static void ElementToStream(Element element, OutputStream out) { try { DOMSource source = new DOMSource(element); StreamResult result = new StreamResult(out); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.transform(source, result); } catch (Exception ex) { } } }