我试图通过简单的框架对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
请帮忙。
答案 0 :(得分: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"
可能会失败。