JiBX 解组 - 是否可以告诉 JiBX 忽略元素的顺序?

发布于 2024-08-22 08:17:33 字数 1833 浏览 9 评论 0原文

有办法解决这个问题吗?

例如,我的 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:预期“group”结束标记,找到“idExt”开始标记(第 4 行,列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 是否可以忽略标签顺序?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-08-29 08:17:33

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> 

有关详细信息,请参阅 JiBX 文档

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文