Java tutorial
//package com.java2s; import java.io.*; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.parsers.ParserConfigurationException; public class Main { public static String serializerXmlString(Object xmlObj) throws JAXBException, ParserConfigurationException { ByteArrayOutputStream baos = (ByteArrayOutputStream) serializerOutputStream(xmlObj); byte[] bystr = baos.toByteArray(); String str = ""; try { str = new String(bystr, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return str; } public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(xmlObj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(xmlObj, baos); return baos; } }