Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Marshall result into String

Tags:

java

jaxb

JAXBContext context = JAXBContext                     .newInstance(CreateExemptionCertificate.class);             Marshaller m = context.createMarshaller();             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);              m.marshal(cc, System.out); 

In the code above i am getting the result to the console (I mean XML is getting printed on the console). I want to get this XML to a string. I am not getting which argument I should pass to the marshal method to get XML String in a String variable instead of printing it on the console. Anybody having any idea please share.

like image 995
Sunny Gupta Avatar asked Feb 05 '12 18:02

Sunny Gupta


1 Answers

You can do it like this :

    CreateExemptionCertificate cc = ...;     JAXBContext context = JAXBContext.newInstance(CreateExemptionCertificate.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(); 
like image 133
Radouane ROUFID Avatar answered Sep 21 '22 10:09

Radouane ROUFID