Java tutorial
//package com.java2s; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.StringWriter; public class Main { public static <T> String marshal(T object) throws JAXBException { final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller(); final StringWriter stringWriter = new StringWriter(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, stringWriter); return stringWriter.toString(); } }