JiBX - Marshal document to string instead of file

C

cmaksymchuk

Hello everyone. I am new to JiBX and so far really love it. Simple to
use, fast, etc. The problem I am having is that I need the
IMarshallingContext object to marshall the object into a String, not to
a file. How would I go about doing this?

Here is my code:
// ==================================================
IBindingFactory bfact = BindingDirectory.getFactory(JdeRequest.class);

// marshal object back out to file (with nice indentation, as UTF-8)
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
FileOutputStream out = new FileOutputStream("out.xml");
mctx.marshalDocument(jdeRequest, "UTF-8", null, out);
// here is where I would like to get the marshalled document in a local
// string variable
// String s = ?
// ==================================================

Please help :)

Thanks in advance to you kind people out there that help unfortunate
souls like me :)

Cheers,

Yeuker
 
T

tom fredriksen

Hello everyone. I am new to JiBX and so far really love it. Simple to
use, fast, etc. The problem I am having is that I need the

I have a question for you.
I dont know JiBX, so I had a look at the webpage, but it was a bit
unclear to me what it is. AFAICT, its a tool to allow "serialisation" of
the data in an object into an xml representation, with tools to help you
control and enhance the transformation. Is this correct?

/tom
 
C

cmaksymchuk

Hi Tom,

JiBX allows you to bind xml to java objects and java objects to xml.
This allows for a seamless, fast transition between the two without
ever having to write code. From the JiBX site: "The JiBX framework
handles all the details of converting your data to and from XML based
on your instructions"

Quite cool actually. There are many, many tools around for doing this,
but JiBX is one of the fastest, most popular, stable... and free out
there. I answered my own question by the way:

IBindingFactory bfact =
BindingDirectory.getFactory(JdeRequest.class);
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
StringWriter stringWriter = new StringWriter();
mctx.setOutput(stringWriter);
mctx.marshalDocument(jdeRequest);
String request = stringWriter.toString();
System.out.println(request);

Hope this helps.

Cheers,

Cory
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top