3

シンプルなフレームワークを使用して xml を逆シリアル化しようとしています。実行時にのみ型がわかる 2 つのリストがあります。そこで、@ElementListUnion を使用しました。

Customer.java

@ElementListUnion({@ElementList(inline = true,type=Thing.class),@ElementList(inline = true,type=AnotherThing.class)})
List<Object> things;


@ElementListUnion({@ElementList(inline = true,type=Thing.class),@ElementList(inline = true,type=AnotherThing.class)})
List<Object> anotherthings ;

しかし、次の例外が発生します

03-20 19:36:20.534: E/AndroidRuntime(2764): Caused by:  
 org.simpleframework.xml.core.PersistenceException: Duplicate annotation of name 
'thing' on @org.simpleframework.xml.ElementListUnion(value=
[@org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true,
  name=, 
required=true, type=class com.data.Thing),  
 @org.simpleframework.xml.ElementList(data=false,
 empty=true, entry=, inline=true, name=, required=true, type=class 
 com.data.AnotherThing)])
 on field 'things' java.util.List com.data.Customer.things

助けてください。

4

1 に答える 1

2

の値を設定していないためentry、Simple は使用しているクラスを判断できません。ここを参照してください:

@Root(name = "Customer")
public class Customer
{
    @ElementListUnion(
    {
        @ElementList(entry = "thingValue", inline = true, type = Thing.class),
        @ElementList(entry = "anotherThingValue", inline = true, type = AnotherThing.class)
    })
    List<Object> things;


    @ElementListUnion(
    {
        @ElementList(entry = "thingValue", inline = true, type = Thing.class),
        @ElementList(entry = "anotherThingValue", inline = true, type = AnotherThing.class)
    })
    List<Object> anotherthings;

}

それぞれ@ElementListに が必要entryです。これは、要素に使用されるタグです。

<Customer>
   <thingValue>...<thingValue/>                 <!-- That's a 'Thing' -->
   <anotherThingValue>...<anotherThingValue/>   <!-- That's an 'AnotherThing' -->
</Customer>

entryただし、クラスのような名前を付けないでentry = "thing"ください。失敗する可能性があります。

于 2013-03-25T22:20:23.443 に答える