2013-03-20 55 views
3

我想通过简单的框架去序列化XML。我有两个列表,只有在运行时才知道它们的类型。所以我使用@ElementListUnion。简单的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 

请帮助。

回答

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"可能会失败。