2015-06-14 95 views
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对象,并从中获取属性。

任何帮助将不胜感激,我不知道如何使它工作,它似乎应该是如此简单,但我显然缺少的东西。

回答

1

能得到一些帮助,这是什么工作

@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; 

    } 
+0

哎呀,忘了做!我发布后不会让我标记它。感谢您的提醒 – Ben987654