Java tutorial
//package com.java2s; import java.io.IOException; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static String ObjToXml(Object object) throws JAXBException, IOException { return ObjToXml(object, object.getClass()); } @SuppressWarnings("rawtypes") public static String ObjToXml(Object object, Class... classesToBeBound) throws JAXBException, IOException { return ObjToXml(object, false, classesToBeBound); } @SuppressWarnings("rawtypes") public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound) throws JAXBException, IOException { JAXBContext context = JAXBContext.newInstance(classesToBeBound); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat); m.setSchema(null); StringWriter sw = new StringWriter(); m.marshal(object, sw); String result = sw.toString(); sw.close(); return result; } }