Android, simple-xml, how to declare a list of elements?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 17:34:59

问题


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


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/7001052/android-simple-xml-how-to-declare-a-list-of-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!