Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static String beanToXml(Object to) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); JAXBContext context = JAXBContext.newInstance(to.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(to, out); return new String(out.toByteArray(), "UTF-8"); } catch (JAXBException | UnsupportedEncodingException e) { e.printStackTrace(); } return null; } }