Java tutorial
//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 { 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); } } }