Simple XML ElementListUnion - two generic lists not allowed?

时间秒杀一切 提交于 2019-12-10 19:18:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!