问题
I am trying to desrialize an xml through simple framework. I have two lists whose types will be known only at runtime. So I used @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 ;
But Im getting the following exception
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
please help.
回答1:
You haven't set a value for entry
, so Simple can't decide which class you are using. See here:
@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;
}
Each @ElementList
requires an entry
, thats the tag which is used for the element:
<Customer>
<thingValue>...<thingValue/> <!-- That's a 'Thing' -->
<anotherThingValue>...<anotherThingValue/> <!-- That's an 'AnotherThing' -->
</Customer>
But make shure you dont name the entry
like the class, so entry = "thing"
may fail.
来源:https://stackoverflow.com/questions/15533272/simple-xml-elementlistunion-two-generic-lists-not-allowed