Java tutorial
//package com.java2s; import java.io.StringWriter; import javax.xml.transform.OutputKeys; 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; public class Main { /** * Get DOM as a string * @param doc * @return */ public static String getStringFromDoc(org.w3c.dom.Document doc) { if (doc == null) { System.out.println("XML document is null"); } try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://commons.omniupdate.com/dtd/standard.dtd"); transformer.transform(domSource, result); writer.flush(); return writer.toString(); } catch (TransformerException ex) { System.out.println("Transformer Exception"); // ex.printStackTrace(); return "Error in transformation"; } } }