6

これを回避する方法はありますか?

たとえば、私の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がタグの順序を無視することは可能ですか?

4

1 に答える 1

10

バインディングのマッピング要素にを追加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のドキュメントを参照してください。

于 2010-02-19T13:18:44.170 に答える