Java - Getting a ElementList attribute with SimpleXML (Android/Retrofit) -
i'm trying parse xml response looks simplexml. it's similar example shown @ simple xml tutorial page
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#javabean
<response> <version>1.0</version> <code>1</code> <message>report generated</message> <records total="365"> <record rowid="1" data1="1234" data2="abc" /> <record rowid="2" data1="5678" data2="def" /> <record rowid="3" data1="9012" data2="ghi" /> </records> </response>
the difference have, <records total="365">
tag has attribute need collect can determine if there's multiple pages of results.
i've tried using example, resulted in this
public class response { private arraylist<record> records; @element public string message; @element public string code; @element public string version; @elementlist public void setrecords(arraylist<record> records) { if(records.isempty()) { throw new illegalargumentexception("empty collection"); } this.records = records; } @elementlist public arraylist<record> getrecords() { return records; } public class record { @attribute public string data1; @attribute public string data2; } }
aside missing total attribute in records tag, works correctly.
whatever try total tag out doesn't work though.
i've tried sorts of combinations of making records class holds attribute , arraylist instead, having in main object basic attribute, or trying have getter / setter in main response object without luck.
e.g.
public class response { @element public string message; @element public string code; @element public string version; @element public records records; public class records{ private arraylist<record> records; @attribute public string total; @elementlist public void setrecords(arraylist<record> records) { if(records.isempty()) { throw new illegalargumentexception("empty collection"); } this.records = records; } @elementlist public arraylist<record> getrecords() { return records; } } public class record { @attribute public string data1; @attribute public string data2; } }
i don't understand how make list object, , attribute it.
any appreciated, i'm not sure how make work, seems should simple, i'm missing something.
was able , works
@default(defaulttype.field) public class response{ public string message; public string code; public string version; public records records; public records getrecords () { return records; } public void setrecords (arraylist<record> records) { this.records.setrecord(records); } } public class records { @attribute public string total; public arraylist<record> records; public string gettotal () { return total; } public void settotal (string total) { this.total = total; } @elementlist(inline=true) public arraylist<record> getrecord () { return records; } @elementlist(inline=true) public void setrecord (arraylist<record> record) { this.records = record; } } public class record { @attribute public string data1; @attribute public string data2; }
Comments
Post a Comment