Java tutorial
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "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; public class Main { /** * Converts a DOM document to an xml String. * * @param doc DOM document * @return xml String. * @throws TransformerException */ public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; if (null != xslt) { DOMSource dxsltsource = new DOMSource(xslt); transformer = tf.newTransformer(dxsltsource); } else { transformer = tf.newTransformer(); } transformer.transform(domSource, result); return writer.toString(); } }