Here you can find the source of documentToBytes(Document doc)
public static byte[] documentToBytes(Document doc) throws Exception
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; 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 { public static byte[] documentToBytes(Document doc) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); ByteArrayOutputStream output = new ByteArrayOutputStream(); StreamResult result = new StreamResult(output); transformer.transform(source, result); byte[] bytes = output.toByteArray(); return bytes; }/*from ww w . j a va 2s .co m*/ }