Java tutorial
//package com.java2s; import java.io.OutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static void marshal(Object o, OutputStream os) throws JAXBException { getMarshaller(o.getClass()).marshal(o, os); } private static Marshaller getMarshaller(Class<?> clazz) throws JAXBException { return getContext(clazz).createMarshaller(); } private static JAXBContext getContext(Class<?> clazz) throws JAXBException { return JAXBContext.newInstance(clazz.getPackage().getName(), clazz.getClassLoader()); } }