2013-03-20 44 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"可能會失敗。