Here you can find the source of toXml(Class className, Object object)
public static String toXml(Class className, Object object)
//package com.java2s; //License from project: Apache License import java.io.*; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class Main { public static String toXml(Class className, Object object) { String strXml = ""; StringWriter writer = null; try {// w ww. j a va 2s . c om writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(className); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writer); strXml = writer.toString(); writer.flush(); strXml = strXml.replace("<", "<"); strXml = strXml.replace(">", ">"); } catch (Exception e) { } finally { if (writer != null) { try { writer.close(); writer = null; } catch (Exception e) { } } } return strXml; } }