Here you can find the source of marshal(Object obj, Class> clazz)
public static String marshal(Object obj, Class<?> clazz)
//package com.java2s; //License from project: Open Source License import java.io.StringWriter; import java.util.concurrent.ConcurrentHashMap; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { private static ConcurrentHashMap<String, JAXBContext> contextMap = new ConcurrentHashMap<String, JAXBContext>(); public static String marshal(Object obj, Class<?> clazz) { String result = null;/* ww w. j a v a2 s . com*/ try { JAXBContext context = contextMap.get(clazz.getPackage().getName()); if (context == null) { context = JAXBContext.newInstance(clazz.getPackage().getName()); contextMap.put(clazz.getPackage().getName(), context); } Marshaller m = context.createMarshaller(); StringWriter writer = new StringWriter(); m.marshal(obj, writer); result = writer.toString(); } catch (JAXBException e) { throw new RuntimeException("Can't marshal the XML file, error message: " + e.getMessage()); } return result; } }