Java tutorial
//package com.java2s; import org.w3c.dom.Document; import java.io.Writer; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 { /** * Serialize XML from a {@link Document} to an OutputStream. * @param doc * @param out */ public static void serialiseXml(Document doc, Writer out) { DOMSource domSource = new DOMSource(doc.getDocumentElement()); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = null; try { serializer = tf.newTransformer(); } catch (TransformerConfigurationException e) { } serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "users.dtd"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); try { serializer.transform(domSource, streamResult); } catch (TransformerException e) { } } }