Here you can find the source of toXmlString(T obj, Class
public static <T> String toXmlString(T obj, Class<T> type)
//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 {/*ww w.ja va 2s . c o m*/ JAXBContext jaxbContext = JAXBContext.newInstance(type); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream); return byteArrayOutputStream.toString(); } catch (JAXBException e) { throw new RuntimeException(e); } } }