Java XML Serialization
Java allow you to easily transform various xml input formats into various output formats by using the java.xml.transform package. In this example, we'll see how to parse an xml document into an array of byte.// use this package
import javax.xml.transform.*;
// transforms xml document to byte array
// o_doc is a org.w3c.Document object
Source source = new DOMSource(o_doc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result result = new StreamResult(baos);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
byte[] out = baos.toByteArray();
Java XML Deserialization
The reverse transformation allow you to build a Java Document object from a byte array.// use this package
import javax.xml.transform.*;
// transforms byte array to xml document
// messenger is a byte array
ByteArrayInputStream bais = new ByteArrayInputStream(messenger);
Source source = new StreamSource(bais);
DOMResult result = new DOMResult();
Transformer transformer = factory.newTransformer();
transformer.transform(source,result);
Node out = result.getNode();
Document to byte array, Java Dom to byte array, Java Document serialization, XML to byte array, Byte array to XML, Byte array deserialization, Byte array to Java DOM Object