Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { @SuppressWarnings("unchecked") public static <T> String marshal(T object) throws IOException, JAXBException { Class<T> clzz = (Class<T>) object.getClass(); JAXBContext context; context = JAXBContext.newInstance(clzz); Marshaller m = context.createMarshaller(); ByteArrayOutputStream os = new ByteArrayOutputStream(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(object, os); return os.toString(); } }