使用带有POJO类的Retrofit SimpleXml-Converter解析以下XML

时间:2016-04-28 16:08:17

标签: android xml xml-parsing retrofit2

这是我的xml结构

<downloads>
<item>98cfa929ee93149e245aabf5e4377058</item>
<item>498b513aa646d6ef1c407cbeabf6bd20</item>
<item>13815d2c0dd53a251bb53717b9f43b64</item>
<item>7a0615eb601264bbae0ad510a31fd61d</item>
<item>3157910f72705e60d0d4ba26c3fb0e84</item>
<item>29e037200d14e21b4ccdfbfaaf1621e8</item>
<item>5f5da6505eaa203d65d1a6203dd0e742</item>
<item>98dcbf2a73016cc9e72c3cbc101de151</item>
<item>7b21d3f11bf6b12e4c4abca7004ad80d</item>
<item>116049d5a4e53240b76666c6826fc6d6</item>
<item>c7ec466398f3d8cee56147c696760076</item>
<item>66ce3335de344b32986d2b77ff992ae2</item>
</downloads>

和细节xml

<download>
<item>
<key>9b94e79fecb1f055f279d95e800867f9</key>
<value>
A Jedi uses the Force for knowledge and defense, never for attack.
</value>
</item>
</download>

这是我想从服务器检索并将上面的xml转换为它。

 public class DownloadPayloadCollection {
        // Class log identifier
        public final static String LOG_TAG = DownloadPayloadCollection.class.getSimpleName();
        @Root(name = "downloads")
        @ElementList(inline=true)
        private List<DownloadPayload> result;


        public DownloadPayloadCollection() {
        }

        public List<DownloadPayload> getResult() {
            Log.d(LOG_TAG, "CALLED FROM getResult() " + result.toString());
            return result;
        }
    }

这是应该代表项目元素的类。

@Root(name = "download")
public class DownloadPayload {

    @Element(name = "key", required = false)
    private String key;

    @Element(name = "value", required = false)
    private String value;

    public DownloadPayload() {
    }

    public String getKeyString() {
        return keyString;
    }

    public void setDownloadPayloadKey(String key) {
        this.key = key;
    }

    public String getDownloadPayloadKey() {
        return key;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

我对如何获取项目元素列表感到困惑。我似乎找不到将物品制成POJO的方法,因为它没有结构。如何使用RetrofitSimpleXmlConverter来实现上述目标?

1 个答案:

答案 0 :(得分:5)

您想了解一下如何使用SimpleXML

你的POJO反序列化类是错误的,你想要这样的东西:

@Root(name = "downloads")
public class Downloads {
    @ElementList(entry = "item", inline = true)
    private List<String> items;
}