Java tutorial
//package com.java2s; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import java.io.OutputStream; public class Main { private static Marshaller marshaller = null; private static Unmarshaller unmarshaller = null; private static JAXBContext context = null; public static void marshal(Class<?> klass, Object obj, OutputStream os) throws JAXBException { try { context = JAXBContext.newInstance(klass); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { throw new RuntimeException( "There was a problem creating a JAXBContext object for formatting the object to XML."); } try { marshaller.marshal(obj, os); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } } }