Here you can find the source of toString(JAXBContext context, Object object)
public static String toString(JAXBContext context, Object object) 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 String toString(JAXBContext context, Object object) throws JAXBException { final Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); final StringWriter sw = new StringWriter(); marshaller.marshal(object, sw);//from w w w . ja v a 2 s .c o m return sw.toString(); } }