Android的,简单的XML,如何声明元素的列表? [英] Android, simple-xml, how to declare a list of elements?

查看:216
本文介绍了Android的,简单的XML,如何声明元素的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据结构:

<estates version="0.1">

 <provider name="Foo">
  <estate id="12345678">
   <description>aaa</description>
   <size>300</size>
  </estate>
  <estate id="12345679">
   <description>bbb</description>
   <size>450</size>
  </estate>
 </provider>

 <provider name="Bar">
  <estate id="987654321">
   <description>yyy</description>
   <size>100</size>
  </estate>
  <estate id="987654320">
   <description>zzz</description>
   <size>240</size>
  </estate>
 </provider>

</estates>

这是我从我的网络服务获得。 我想实例化机器人遗产,供应商和地产类,使用简单的XML库。

which I get from a web service of mine. I would like to instantiate Android "Estates", "Provider", and "Estate" classes, using simple-xml library.

类Estates.java:

Class Estates.java:

@Root
public class Estates {
   @ElementList
   private List<Estate> providers;

   @Attribute
   private String version;
}

类Estate.java:

Class Estate.java:

public class Estate { 
   @Attribute
   private String id;

   @Element(required=false)
   private String description;

   @Element(required=false)
   private Integers size;
}

也就是说,我用一个@ElementList(名单&LT;房地产&GT;供应商)的供应商,但这种方式我只可以使用的一个的提供者列表(有两个列表,因为在我的例子,给出了: )ERROR / com.simplexml.XmlActivity(4266):未捕获的异常:org.simpleframework.xml.core.PersistenceException元素'供应商'在第1行声明了两次。更多的,我不能让名属性。在实践中,我想我必须摒弃列出供应商的做法,但如何更换呢?我应该使用一个集合?

I.e., I use an @ElementList (List<Estate> providers) for the providers, but this way I can only use one provider list (having two lists, as in my example, gives: "ERROR/com.simplexml.XmlActivity(4266): Uncaught exception: org.simpleframework.xml.core.PersistenceException: Element 'providers' declared twice at line 1"). More, I can't get "name" attribute. In practice, I suppose I have to dismiss the "List providers" approach, but how to replace it? Should I use a "Collection"?

推荐答案

需要注意的是简单的XML也允许你建立的POJO以这样的方式来让你有名单在网上。在其他的话,你可以走与&LT; providersList&GT; 元素,仍然数组&LT;供应商&GT; 旨意被识别出来。

Note that simple-xml also allows you to set up the POJOs in such a way as to allow you to have the list in-line. On other words, you can do away with the <providersList> element and still the array of <provider>s will be recognizable.

当你接收过XML的结构没有控制这一点很重要。在你的情况,如果你坚持XML定义按原来的问题,你可以将其标注为这样的:

This is important when you have no control over the structure of the XML being received. In your case, if you stuck to the XML definition as per the original question, you would annotate it thus:

@ElementList(inline=true)
   private List<Estate> providers;

这篇关于Android的,简单的XML,如何声明元素的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆