When working with JAXB, sometimes you want a String representation of your XML. This tutorial will show how to JAXB marshal to String.
Below the code of marshaling to String.
1 2 3 4 5 6 7 8 9 |
ClassWeWantToMarhsall cc = ...; JAXBContext context = JAXBContext.newInstance(ClassWeWantToMarhsall.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter sw = new StringWriter(); m.marshal(cc, sw); String result = sw.toString() |