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

本文是小编为大家收集整理的关于Android,simple-xml,如何声明一个元素的列表?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有这个数据结构:

<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>

我从我的Web服务中获得. 我想使用简单XML库实例化Android"庄园","提供商"和"遗产"类.

类estates.java:

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

   @Attribute
   private String version;
}

class estate.java:

public class Estate { 
   @Attribute
   private String id;

   @Element(required=false)
   private String description;

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

IE,我为提供程序使用@ElementList(List&lt;Estate&gt; providers),但这种方式我只能使用一个提供程序列表(具有两个列表,如我的示例中,给出:

"错误/com.simplexml.xmlactity(4266):未捕获的例外:org.simpleframework.xml.core.persistenceException:元素'提供程序'在第1行中声明两次").

更多,我无法获得"名称"属性.在实践中,我想我必须解雇"List<Estate> providers"方法,但如何替换它?我应该使用"集合"?

推荐答案

注意简单XML还允许您以允许您在线列表的方式设置POJO.在其他单词上,您可以使用<providersList>元素,仍将识别<provider> s的阵列.

当您无法控制接收的XML的结构时,这是重要的.在您的情况下,如果您根据原始问题困扰着XML定义,因此您将注释:

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

其他推荐答案

我回答我自己的问题,说明我可能有一个错误的XML定义,应该是:

<estates version="0.1">
 <providerslist>
  <provider name="Foo">
   <estateslist>
    <estate id="12345678">
     <description>aaa</description>
     <size>300</size>
    </estate>
    <estate id="12345679">
     <description>bbb</description>
     <size>450</size>
    </estate>
   </estateslist>
  </provider> 
  <provider name="Bar">
   <estateslist>
    <estate id="987654321">
     <description>yyy</description>
     <size>100</size>
    </estate>
     <estate id="987654320">
     <description>zzz</description>
     <size>240</size>
    </estate>
   </estateslist>
  </provider>
 </providerslist>
</estates>
这种方式我可以轻松解析为"遗产","提供商"和"遗产"类,其中前两个具有包含内部列表的元素列表.

这将关闭问题.

本文地址:https://www.itbaoku.cn/post/102391.html

问题描述

I have this data structure:

<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>

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

Class Estates.java:

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

   @Attribute
   private String version;
}

Class Estate.java:

public class Estate { 
   @Attribute
   private String id;

   @Element(required=false)
   private String description;

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

I.e., I use an @ElementList (List&lt;Estate&gt; 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<Estate> providers" approach, but how to replace it? Should I use a "Collection"?

推荐答案

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.

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;

其他推荐答案

I answer my own question to state I probably had a mistake with the xml definition, which should be:

<estates version="0.1">
 <providerslist>
  <provider name="Foo">
   <estateslist>
    <estate id="12345678">
     <description>aaa</description>
     <size>300</size>
    </estate>
    <estate id="12345679">
     <description>bbb</description>
     <size>450</size>
    </estate>
   </estateslist>
  </provider> 
  <provider name="Bar">
   <estateslist>
    <estate id="987654321">
     <description>yyy</description>
     <size>100</size>
    </estate>
     <estate id="987654320">
     <description>zzz</description>
     <size>240</size>
    </estate>
   </estateslist>
  </provider>
 </providerslist>
</estates>

This way I can easily parse it to "Estates", "Providers" and "Estate" classes, the first two having an ElementList to contain the inner list.

This closes the question.