Here you can find the source of marshallToString(Object jaxbElement, Class
public static <T> String marshallToString(Object jaxbElement, Class<T> c) throws JAXBException, IOException
//package com.java2s; //License from project: GNU General Public License import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { private static JAXBContext jc = null; private static Marshaller m = null; public static <T> String marshallToString(Object jaxbElement, Class<T> c) throws JAXBException, IOException { initMarshaller(c);//from w w w .j av a 2 s .co m ByteArrayOutputStream fos = new ByteArrayOutputStream(); m.marshal(jaxbElement, fos); fos.flush(); String res = fos.toString("UTF-8"); fos.close(); return res; } private static final <T> void initMarshaller(Class<T> c) throws JAXBException { jc = JAXBContext.newInstance(new Class[] { c }); m = jc.createMarshaller(); } }