【问题标题】:Java - Getting a ElementList attribute with SimpleXML (Android/Retrofit)Java - 使用 SimpleXML (Android/Retrofit) 获取 ElementList 属性
【发布时间】: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>

我唯一的区别是我的&lt;records total="365"&gt; 标签有一个我需要收集的属性,以便我可以确定是否有多页结果。

我尝试使用他们的示例,结果是这样的

  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


    【解决方案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;
    
        }
    

    【讨论】:

    • 糟糕,忘记了!它不会让我一发布就标记它。谢谢提醒
    猜你喜欢
    • 2013-08-19
    • 2012-10-03
    • 1970-01-01
    • 2011-09-17
    • 2012-05-19
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    相关资源
    最近更新 更多