Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Node; public class Main { private static final TransformerFactory FACTORY = TransformerFactory.newInstance(); public static byte[] xmlToBytes(final Node body) throws TransformerException { final Transformer transformer = FACTORY.newTransformer(); final Source source = new DOMSource(body); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final Result result = new StreamResult(buffer); transformer.transform(source, result); return buffer.toByteArray(); } }