Here you can find the source of document2XmlString(Document xmldoc)
public static String document2XmlString(Document xmldoc)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import javax.xml.transform.Source; 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 { public static String document2XmlString(Document xmldoc) { try {/* w w w.j a v a2s .co m*/ Source src = new DOMSource(xmldoc); ByteArrayOutputStream bout = new ByteArrayOutputStream(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // identity transformer transformer.transform(src, new StreamResult(bout)); return bout.toString("UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } }