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

1

6

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?

Bettor answered 19/2, 2010 at 12:58 Comment(0)
K
10

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.

Kalimantan answered 19/2, 2010 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.