Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static String marshal(Object o) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshal(o, bos, new HashMap<String, Object>()); return bos.toString(); } public static String marshal(Object o, Map<String, Object> properties) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); marshal(o, bos, properties); return bos.toString(); } public static void marshal(Object o, OutputStream os) { marshal(o, os, new HashMap<String, Object>()); } public static void marshal(Object o, OutputStream os, Map<String, Object> properties) { try { JAXBContext ctx = JAXBContext.newInstance(o.getClass()); Marshaller marshaller = ctx.createMarshaller(); for (Entry<String, Object> p : properties.entrySet()) { marshaller.setProperty(p.getKey(), p.getValue()); } marshaller.marshal(o, os); } catch (JAXBException e) { throw new RuntimeException(e); } } }