0

How should I create classes for this kind of xml response?

<delivery_zones>
            <zone>3</zone>
            <zone_name>....</zone_name>
            <zone>10</zone>
            <zone_name>....</zone_name>
            <zone>7</zone>
            <zone_name>....</zone_name>
            <zone>9</zone>
            <zone_name>....</zone_name>
            <zone>13</zone>
            <zone_name>....</zone_name>
</delivery_zones>

Thank you..

KHALED
  • 571
  • 1
  • 5
  • 14
  • DeliveryZones with list of zones and zone_name – Raghunandan Sep 11 '17 at 09:32
  • when I am using list of zones, there is always an exception "Element zone is already used"!! – KHALED Sep 11 '17 at 09:48
  • just use online xml to pojo convertor – Raghunandan Sep 11 '17 at 10:07
  • Please give more info about what you need to achieve using this xml. Pojo classes may not help you. It has multiple values with the same xml tag. I think you may need to parse this xml using xml parsing libraries. – K Sathish Sep 11 '17 at 10:07
  • Well, i am using "org.simpleframework.xml" o parse it, but the thing here is that "delivery_zones" has list of same named tages, which makes it harder to parse it. – KHALED Sep 11 '17 at 10:13

4 Answers4

0

Write a Class

class Zone {
   int id;
   String name;

   public(int id, String name) {
       this.id = id;
       this.name = name;
   }
}

Create a list of Zone objects. Parse the response and create a object to Zone class for each zone and add to the list if its not contain that zone.

I think this might help you. If you need more info about xml parsing. This link https://stackoverflow.com/a/5059411/7867325 helps you.

K Sathish
  • 247
  • 3
  • 13
0

This is the whole response

<rss version="2.0" >
<channel>
    <record>46</record>
    <total_record>46</total_record>
    <item>
        <name>1 Can</name>
        <description />
        <delivery_time>24 hours</delivery_time>
        <img_1 />
        <img_2 />
        <img_3 />
        <thumb_1 />
        <thumb_2 />
        <thumb_3 />

        <delivery_zones>
            <zone>5</zone>
            <zone_name>Free Delivery</zone_name>
        </delivery_zones>
        <price_records>1</price_records>

    </item>

    <item>
    ....
    </item>

</channel>
</rss>

and here is my class:

public static class Item {

    @Element(required = false)
    public String name;

    @Element(name = "delivery_zones", required = true)
    public DeliveryZones delivery_zones;


//        @Path(value = "delivery_zones")
//        @ElementList(name = "zone", required = true, inline = true)
//        private List<Integer> zone;
//
//        @Path(value = "delivery_zones")
//        @ElementList(name = "zone_name", required = true, inline = true)
//        private List<String> zoneName;


//        @Path(value = "delivery_zones")
//        @ElementList(name = "zone", required = true, inline = true)
//        public List<Integer> ids;
//
//        @Path(value = "delivery_zones")
//        @ElementList(name = "zone_name", required = true, inline = true)
//        public List<String> names;
}
KHALED
  • 571
  • 1
  • 5
  • 14
0

in case anyone is facing same issue, here is how to solve it:

public static class Zone {
    @Element(name = "zone", required = false)
    public String zone;
}

public static class ZoneName {
    @Element(name = "zone_name", required = false)
    public String zone_name;
}


public static class DeliveryZones {

    @ElementList(entry = "zone", inline = true)
    public List<Zone> zones;

    @ElementList(entry = "zone_name", inline = true)
    public List<ZoneName> zone_name;
}
KHALED
  • 571
  • 1
  • 5
  • 14
0

Another solution

@Root
public static class DeliveryZones {

    @ElementList(entry = "zone", inline = true)
    public List<String> zones;

    @ElementList(entry = "zone_name", inline = true)
    public List<String> zone_name;
}

and in the parent class

    @Element(name = "delivery_zones", required = true)
    public DeliveryZones delivery_zones;
KHALED
  • 571
  • 1
  • 5
  • 14