Java - Getting a ElementList attribute with SimpleXML (Android/Retrofit)

2.2k views Asked by At

I'm trying to parse an XML response that looks like this with SimpleXML. It's very similar to the example shown at the 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 only difference I have, is my <records total="365"> tag has an attribute I need to collect so I can determine if there's multiple pages of results.

I've tried using their example, which 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 from missing the Total attribute in the Records tag, this works correctly.

Whatever I try to do to get this Total tag out doesn't work though.

I've tried all sorts of combinations of making a Records class that holds the attribute and ArrayList instead, having it in the main object as a basic attribute, or trying to have a getter / setter in the main response object without any 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 to make the List object, and get an attribute from it.

Any help would be greatly appreciated, I'm not sure how to make it work, it seems like it should be so simple, but I'm obviously missing something.

1

There are 1 answers

1
Ben987654 On BEST ANSWER

Was able to get some help and this is what 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;

    }