Here you can find the source of XmlObjectToString(Object o)
public static String XmlObjectToString(Object o)
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; import java.io.OutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { public static String XmlObjectToString(Object o) { OutputStream baos = new ByteArrayOutputStream(); try {//from w w w. ja v a 2s .c o m JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(o, baos); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return baos.toString(); } }