0
我試圖解析看起來像這樣的SimpleXML的XML響應。這是非常相似,在簡單的XML教程頁面Java - 使用SimpleXML獲取ElementList屬性(Android/Retrofit)
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>
我唯一的區別所示的例子,是我<records total="365">
標籤有我需要收集這樣我就可以判斷是否有多個頁面的一個屬性的結果。
我使用他們的榜樣,這導致該
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;
}
}
除了失蹤的記錄標記的總屬性試過了,這正常工作。
無論我試圖做些什麼來獲得這個Total標籤都不起作用。
我試過各種組合的記錄類,它擁有屬性和ArrayList,而不是主要對象作爲基本屬性,或試圖在主響應對象中有一個getter/setter運氣好的話。
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;
}
}
我不明白如何讓List對象,並從中獲取屬性。
任何幫助將不勝感激,我不知道如何使它工作,它似乎應該是如此簡單,但我顯然缺少的東西。
哎呀,忘了做!我發佈後不會讓我標記它。感謝您的提醒 – Ben987654