Java tutorial
//package com.java2s; import java.io.OutputStream; import org.w3c.dom.Document; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; public class Main { public static boolean writeDocumentToStream(Document doc, OutputStream os) { // Check if DOM Load and Save is supported if (!((doc.getFeature("Core", "3.0") != null) && (doc.getFeature("LS", "3.0") != null))) throw new RuntimeException("DOM Load and Save unsupported"); // Grab the available implementation DOMImplementationLS DOMiLS = (DOMImplementationLS) (doc.getImplementation()).getFeature("LS", "3.0"); // Create LS output destination LSOutput lso = DOMiLS.createLSOutput(); lso.setByteStream(os); // Create a LS serializer // and tell it to make the output 'pretty' LSSerializer serializer = DOMiLS.createLSSerializer(); serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Serialize the xml to your output stream return serializer.write(doc, lso); } }