Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JiBX unmarshalling - is it possible to tell JiBX to ignore the order of elements?

Is there a way to get by this?

For example, my XML:

<group>
    <idExt>new group idext</idExt>
    <user-id>1</user-id>
    <parent-id>2</parent-id>        
</group>

when unmarshalling, goes without errors, but when I change order:

<group>
    <user-id>1</user-id>
    <parent-id>2</parent-id>
    <idExt>new group idext</idExt>
</group>

it fails org.jibx.runtime.JiBXException: Expected "group" end tag, found "idExt" start tag (line 4, col 2).

My unmarshalling (implementing Struts2 ContentTypeHandler interface):

public void toObject(Reader in, Object target) {
    try {
        IBindingFactory bf = BindingDirectory.getFactory(target.getClass());
        IUnmarshallingContext umc = bf.createUnmarshallingContext(); 
        umc.setDocument(in); 
        // This un-conditional cast is the current way that JibX unmarshalls to an // already instantiated object - YUCK 
        ((IUnmarshallable)target).unmarshal(umc); 
    } catch (JiBXException e) {
        throw new RuntimeException(e);
    }
}

And binding:

<binding>       
    <mapping name="group" class="GroupVO" >
        <value name="id" field="id" usage="optional"/>
        <value name="idExt" field="idExt" usage="optional"/>
        <value name="active" field="active" usage="optional"/>
        <value name="created-at" field="dateCre" usage="optional"/>
        <value name="updated-at" field="dateChg" usage="optional"/>
        <value name="deleted-at" field="dateDel" usage="optional"/>
        <value name="user-id" field="userId" usage="optional" />
        <value name="parent-id" field="parentId" usage="optional" />
    </mapping>
</binding>

So, is possible for JiBX to ignore tag order?

like image 586
Trick Avatar asked Feb 19 '10 12:02

Trick


1 Answers

Add an ordered="false" to your mapping element in the binding:

<binding>        
    <mapping name="group" class="GroupVO" ordered="false"> 
        <value name="id" field="id" usage="optional"/> 
        <value name="idExt" field="idExt" usage="optional"/> 
        <value name="active" field="active" usage="optional"/> 
        <value name="created-at" field="dateCre" usage="optional"/> 
        <value name="updated-at" field="dateChg" usage="optional"/> 
        <value name="deleted-at" field="dateDel" usage="optional"/> 
        <value name="user-id" field="userId" usage="optional" /> 
        <value name="parent-id" field="parentId" usage="optional" /> 
    </mapping> 
</binding> 

For more info, see the documentation for JiBX.

like image 94
Cheeso Avatar answered Sep 28 '22 19:09

Cheeso