Java tutorial
//package com.java2s; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; 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 { private static String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; public static String documentToString(Document doc, String xslName) { StringWriter sw = new StringWriter(); try { TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc.getDocumentElement()); Result result = new StreamResult(sw); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } return head + xslName + sw.toString(); } }