Java tutorial
//package com.java2s; import java.io.StringWriter; import java.io.Writer; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static String objectToXML(Class clazz, Object object) throws JAXBException { String xml = null; JAXBContext context = JAXBContext.newInstance(clazz); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Writer w = new StringWriter(); m.marshal(object, w); xml = w.toString(); return xml; } }