Here you can find the source of marshal(Class
@SuppressWarnings("unchecked") public static <T> T marshal(Class<T> c, String xml) throws JAXBException
//package com.java2s; //License from project: Apache License import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Main { @SuppressWarnings("unchecked") public static <T> T marshal(Class<T> c, String xml) throws JAXBException { T res;//w ww .java 2 s . c o m if (c == xml.getClass()) { res = (T) xml; } else { JAXBContext ctx = JAXBContext.newInstance(c); Unmarshaller marshaller = ctx.createUnmarshaller(); res = (T) marshaller.unmarshal(new StringReader(xml)); } return res; } @SuppressWarnings("unchecked") public static <T> String unmarshal(Class<T> c, Object o) throws Exception { JAXBContext ctx = JAXBContext.newInstance(c); Marshaller marshaller = ctx.createMarshaller(); StringWriter entityXml = new StringWriter(); marshaller.marshal(o, entityXml); String entityString = entityXml.toString(); return entityString; } }