Java tutorial
//package com.java2s; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import java.io.ByteArrayOutputStream; public class Main { public static <T> String toXmlString(T obj, Class<T> type) { try { JAXBContext jaxbContext = JAXBContext.newInstance(type); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream); return byteArrayOutputStream.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } }