Simple XML ElementListUnion - Are Two Shared Lists Not Allowed?

I am trying to describe xml through a simple structure. 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.

+3
source share
1 answer

You have not set a value for entry, so Simple cannot 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;

}

For each , it is @ElementListrequired entry, that is, the tag that is used for the element:

<Customer>
   <thingValue>...<thingValue/>                 <!-- That a 'Thing' -->
   <anotherThingValue>...<anotherThingValue/>   <!-- That an 'AnotherThing' -->
</Customer>

, entry , entry = "thing" .

+2

Source: https://habr.com/ru/post/1545249/


All Articles