Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 816
MarcoS Avatar asked Aug 09 '11 18:08

MarcoS


1 Answers

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;
like image 75
curioustechizen Avatar answered Nov 12 '22 10:11

curioustechizen