Java tutorial
//package com.java2s; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; public class Main { public static <T> String obj2Xml(T c) throws RuntimeException { String returnXml = ""; Writer writer = null; try { writer = new StringWriter(); JAXBContext.newInstance(c.getClass()).createMarshaller().marshal(c, writer); returnXml = writer.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } finally { try { writer.close(); } catch (IOException e) { throw new RuntimeException(e); } } return returnXml; } }