ElementListUnion - Simple xml giving duplicate annotation for generic list objects

人走茶凉 提交于 2019-12-13 16:37:39

问题


I am trying to deserialize a List field using elementlistunion

Customer.java

 @ElementListUnion({ @ElementList(inline = false, type = Thing.class),
        @ElementList(inline = false, type = AnotherThing.class) })
List<Object> things = new ArrayList<Object>();

where Thing and AnotherThing are 2 POJO's.But Iam getting the following exception

03-21 18:56:31.940: E/AndroidRuntime(2289): Caused by:
org.simpleframework.xml.core.PersistenceException: Duplicate annotation
of name 'things' on
@org.simpleframework.xml.ElementListUnion(value=
[@org.simpleframework.xml.ElementList(data=false,
 empty=true, entry=, inline=false, name=, required=true, type=class
 com.data.Thing), @org.simpleframework.xml.ElementList(data=false,
 empty=true, entry=, inline=false, name=, required=true, type=class
 com.data.AnotherThing)]) on field 'things' java.util.List
 com.data.Customer.things

I have been stuck on this for a whole day now. Please Help.

Here is my response xml :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
  <address>
     <no>122</no>
      <street>xxx</street>
  </address>
  <id>122</id>
  <name>James Bond</name>
  <things>
      <thing>
        <val>185</val>
      </thing>
      <thing>
        <val>162</val>
      </thing>
   </things>


回答1:


I don't know why are you trying to make two classes in ElementsListUnion. You can use

@ElementList(inline = false, type = Thing.class) 

instead of this or

 @ElementListUnion({ @ElementList(inline = false, type = Thing.class, required = false),
        @ElementList(inline = false, type = AnotherThing.class, required = false) }) 

if you don't send two elements types every time.



来源:https://stackoverflow.com/questions/15556001/elementlistunion-simple-xml-giving-duplicate-annotation-for-generic-list-objec

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