Here you can find the source of marshal(Class
public static <T> String marshal(Class<T> clazz, T object) throws JAXBException, PropertyException
//package com.java2s; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.PropertyException; import javax.xml.namespace.QName; public class Main { public static <T> String marshal(Class<T> clazz, T object) throws JAXBException, PropertyException { StringWriter writer = new StringWriter(); JAXBContext jc = JAXBContext.newInstance(clazz); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(new JAXBElement<T>(new QName("body"), clazz, object), writer); String actual = writer.toString(); return actual; }/* w w w .ja va 2s. c o m*/ }