Thursday, June 18, 2009

JiBX woes

Couldn't find a solution to the below mentioned exception while using JiBX.
Mr. N (http://bit.ly/9Ttv7) had a solution up but that was the only one I found. So adding this here again to increase the number :)

Exception
org.jibx.runtime.JiBXException: Supplied root object of class java.util.Vector cannot be marshalled without top-level mapping
at org.jibx.runtime.impl.MarshallingContext.marshalRoot(MarshallingContext.java:1023)
at org.jibx.runtime.impl.MarshallingContext.marshalDocument(MarshallingContext.java:1083)
at com.offguard.bean.content.ContentManager.marshall(ContentManager.java:39)
at com.offguard.bean.content.ContentManager.run(ContentManager.java:55)
at java.lang.Thread.run(Unknown Source)

Solution

I had written the following code

Vector vec = new Vector(); // this holds all the objects to be marshalled
CollectionWrapper wrapper = new CollectionWrapper(vec);
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(4);
mctx.marshalDocument(vec, "UTF-8", null,new FileOutputStream("filename"));

Instead of using vec in calling mctx.marshalDocument(...) method, I should have used the wrapper object. The modified code is

mctx.marshalDocument(wrapper, "UTF-8", null,new FileOutputStream("filename"));

P.S. :Don't forget to update the binding.xml using the collections tag.

No comments: