Here you can find the source of marshall(Object obj)
public static String marshall(Object obj) throws JAXBException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static String marshall(Object obj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter w = new StringWriter(); m.marshal(obj, w);//from w ww . j a v a 2 s. com return w.toString(); } public static File marshall(Object obj, File file) throws IOException, JAXBException { if (!file.exists() && !file.createNewFile()) throw new IOException("Can't create " + file); if (!file.canWrite()) throw new IOException("Can't write to " + file); JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); FileWriter w = new FileWriter(file); m.marshal(obj, w); w.close(); return file; } }