3

我正在尝试通过简单的框架反序列化 xml。我有两个列表,它们的类型仅在运行时才知道。所以我使用了@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 回答