Java tutorial
//package com.java2s; import org.w3c.dom.Document; import java.io.OutputStream; import javax.xml.transform.Result; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; public class Main { public static void DocumentToStream(final Document doc, OutputStream stream) { Result l_s = new StreamResult(stream); doc.normalize(); try { TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s); stream.close(); } catch (Exception e) { System.err.println(e); return; } } }