2013-04-11 132 views
1

我正在开发一个Android项目,它使用API​​来获取它的数据。现在首先,我不能改变API的任何东西,因为它也用于已经启动的iPhone应用程序。所以我必须解决这个问题。Xstream Ambigious xml标签

我想使用XStream从API中读取XML。一切进展顺利,XStream的工作非常简单。直到我偶然发现一个带有ambigious标签的API调用。这是由API返回的XML是如下:

<response> 
    <plant> 
     <Name /> 
     <Description /> 
     <KeyValues> 
      <entry> 
       <Key /> 
       <Value /> 
      </entry> 
      <entry> 
       <Key /> 
       <Value /> 
      </entry> 
      <entry> 
       <Key /> 
       <Value /> 
      </entry> 
     </KeyValues> 
     <Tasks> 
      <entry> 
       <Title /> 
       <Text /> 
      </entry> 
      <entry> 
       <Title /> 
       <Text /> 
      </entry> 
     </Tasks> 
    </plant> 
</response> 

正如你可以看到两个标签键值来作为标记任务包含的条目标签。我遇到的问题是我无法专门将入口标签别名到我拥有的java类。我的工厂类如下所示:

public class Plant extends BaseModel { 
    private String Name; 
    private String Description; 

    private List<KeyValue> KeyValues; 
    private List<Task> Tasks; 
} 

其中KeyValue和Task类基本上是两个输入类。但是当我尝试反序列化XML我收到以下错误:

com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.Map$Entry as it does not have a no-args constructor : Cannot construct java.util.Map$Entry as it does not have a no-args constructor 
---- Debugging information ---- 
message    : Cannot construct java.util.Map$Entry as it does not have a no-args constructor 
cause-exception  : com.thoughtworks.xstream.converters.reflection.ObjectAccessException 
cause-message  : Cannot construct java.util.Map$Entry as it does not have a no-args constructor 
class    : java.util.Map$Entry 
required-type  : java.util.Map$Entry 
converter-type  : com.thoughtworks.xstream.converters.reflection.ReflectionConverter 
ath    : /response/plant/KeyValues/entry 
line number   : 1 
class[1]   : java.util.ArrayList 
converter-type[1] : com.thoughtworks.xstream.converters.collections.CollectionConverter 
class[2]   : com.example.android.stadseboeren.model.Plant 
version    : 0.0 
------------------------------- 

我得到的事实是,在一个XML使用模棱两可的标签是不理想的情况,但没有什么我可以做,现在去改变它。

有没有人可以帮我解决这个问题?

干杯大安

回答

2

OK,所以要成为一个好公民,因为我想通了,我会在这里发布答案。

最终我最终创建了一个额外的类,它基本上只是参赛名单的持有者。

public class KeyValues extends BaseModel { 

    @XStreamImplicit(itemFieldName="entry") 
    private ArrayList<KeyValueEntry> entries; 
} 

使用XStreamImplicit我可以将条目对象绑定到我的数组列表。

这不是最漂亮的解决方案,但它的工作原理。