Here you can find the source of convertObjectToXML(Object object)
public static String convertObjectToXML(Object object)
//package com.java2s; //License from project: Open Source License import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import java.io.StringWriter; public class Main { public static String convertObjectToXML(Object object) { StringWriter sw = new StringWriter(); String xmlString = null;//from w w w. ja v a2s .com try { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.marshal(object, sw); xmlString = sw.toString(); } catch (JAXBException e) { e.printStackTrace(); } if (xmlString == null) { return ""; } else { return xmlString; } } }