Here you can find the source of marshal(T t, Class
public static <T> String marshal(T t, Class<T> entityClass)
//package com.java2s; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static <T> String marshal(T t, Class<T> entityClass) { StringWriter sw = new StringWriter(); try {/*from ww w .j a v a 2 s. c o m*/ JAXBContext jaxbContext = JAXBContext.newInstance(entityClass); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name()); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(t, sw); } catch (JAXBException e) { throw new RuntimeException(e); } return sw.toString(); } }