Here you can find the source of toXML(Object thing)
public static String toXML(Object thing)
//package com.java2s; //License from project: Open Source License 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 toXML(Object thing) { StringWriter sw = new StringWriter(); try {//from w w w .jav a 2 s . c o m JAXBContext jaxbContext = JAXBContext.newInstance(thing.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(thing, sw); return sw.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } }