Here you can find the source of marshal(Class
public static <T> String marshal(Class<T> clazz, T obj) throws JAXBException
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static <T> String marshal(Class<T> clazz, T obj) throws JAXBException { StringWriter out = new StringWriter(); JAXBContext context = JAXBContext.newInstance(clazz); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(obj, out);/*from w w w . j a v a 2 s.c om*/ return out.toString(); } }