Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; 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 { public static String document2String(Document document, String encode) throws TransformerException { String xml = null; TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); transformer.transform(source, new StreamResult(sw)); xml = sw.toString(); return xml; } }