package test; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.io.StringWriter; import org.junit.Test; public class JAXBTest { @Test public void marshalling() throws Exception { DAO dao = new DAO(); dao.foo = "some unicode characters:\n" + "- 0x09: '\u0009' (tab; allowed)\n" + "- 0x10: '\u0010' (DLE; illegal in XML)\n" + "- 0x20: '\u0020' (space; legal in XML)"; StringWriter writer = new StringWriter(); JAXBContext jc = JAXBContext.newInstance(DAO.class); Marshaller m = jc.createMarshaller(); Unmarshaller um = jc.createUnmarshaller(); // marshal: m.marshal(dao, writer); System.out.println("Marshalled: " + writer.toString()); // unmarshal DAO dao2 = (DAO)um.unmarshal(new StringReader(writer.toString())); System.out.println("Unmarshalled: " + dao2.foo); } @XmlRootElement public static class DAO { public String foo; } }