JiBX unmarshalling - 是否有可能告诉JiBX忽略元素的顺序?

时间:2010-02-19 12:58:42

标签: java tags unmarshalling jibx

有没有办法解决这个问题?

例如,我的XML:

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

当解组时,没有错误,但是当我改变顺序时:

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

它失败了org.jibx.runtime.JiBXException: Expected "group" end tag, found "idExt" start tag (line 4, col 2)

我的解组(实现Struts2 ContentTypeHandler接口):

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);
    }
}

并且绑定:

<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>

那么,JiBX可以忽略标签顺序吗?

1 个答案:

答案 0 :(得分:9)

在绑定中的映射元素中添加ordered="false"

<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> 

有关详细信息,请参阅the documentation for JiBX