【发布时间】:2015-06-14 16:56:55
【问题描述】:
我正在尝试使用 SimpleXML 解析看起来像这样的 XML 响应。它与简单 xml 教程页面中显示的示例非常相似
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;
}
}
除了缺少 Records 标记中的 Total 属性之外,这可以正常工作。
无论我尝试做什么来取出这个 Total 标签都行不通。
我已经尝试了各种组合,例如制作一个包含属性和 ArrayList 的 Records 类,将其作为基本属性放在主对象中,或者尝试在主响应对象中使用 getter/setter 而没有任何运气。
例如
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 对象,并从中获取属性。
任何帮助将不胜感激,我不知道如何使它工作,看起来应该很简单,但我显然错过了一些东西。
【问题讨论】:
标签: java android simple-framework