The following example shows serializing a POJO with binary data to an XML.
The POJO should have a no-arg default constructor and
@XmlRootElement annotation at class level.
public static void main (String [] args) throws JAXBException {
BinaryData binaryData = new BinaryData();
binaryData.setData("BE THE CODER".getBytes()); //encoded in xs:base64Binary by default
/**
* Create JAXB Context from the classes to be serialized
*/
StringWriter output = new StringWriter();
JAXBContext context = JAXBContext.newInstance(BinaryData.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(binaryData, output);
System.out.println(output.toString());
/**
* Unmarshall
*/
Unmarshaller um = context.createUnmarshaller();
StringReader sr = new StringReader(output.toString());
binaryData = (BinaryData) um.unmarshal(sr);
System.out.println("Data decoded : " + new String(binaryData.getData()));