Here you can find the source of objectToXml(Object source, Class... type)
@SuppressWarnings("rawtypes") public static String objectToXml(Object source, Class... type)
//package com.java2s; //License from project: Apache License import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { @SuppressWarnings("rawtypes") public static String objectToXml(Object source, Class... type) { String result;/* w w w .j av a 2s . c o m*/ StringWriter sw = new StringWriter(); try { JAXBContext jaxbContext = JAXBContext.newInstance(type); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(source, sw); result = sw.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } return result; } }