ElementListUnion - 为通用列表对象提供重复注释的简单 xml

2024-03-27

我正在尝试使用 elementlistunion 反序列化列表字段

Customer.java

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

其中 Thing 和 Another Thing 是 2 个 POJO。但是我遇到以下异常

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

我已经被这个问题困扰了一整天了。请帮忙。

这是我的回复 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>

我不知道你为什么要在 ElementsListUnion 中创建两个类。您可以使用

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

而不是这个或

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

如果你不是每次都发送两个元素类型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ElementListUnion - 为通用列表对象提供重复注释的简单 xml 的相关文章

随机推荐