【问题标题】:Simple XML ElementListUnion - two generic lists not allowed?简单的 XML ElementListUnion - 不允许使用两个通用列表?
【发布时间】:2013-03-10 02:54:55
【问题描述】:

我正在尝试通过简单的框架反序列化 xml。我有两个列表,它们的类型仅在运行时才知道。所以我使用了@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 ;

但我得到以下异常

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

请帮忙。

【问题讨论】:

    标签: android xml list unmarshalling simple-framework


    【解决方案1】:

    您尚未为 entry 设置值,因此 Simple 无法确定您使用的是哪个类。见这里:

    @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;
    
    }
    

    每个@ElementList 都需要一个entry,这是用于元素的标签:

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

    但请确保您不要将 entry 命名为类,因此 entry = "thing" 可能会失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多