使用Simple Framework在Java中反序列化简单的XML

时间:2014-09-18 12:15:26

标签: java android xml simple-framework

所以我试图用Simple Framework反序列化java中的一个简单的xml。我的xml是一个巨大的数据,我只发布导致我问题的部分。

<item>
<title>Awesome Title</title>

<link>https://awesome_link</link>

<description>Awesome very very long
Description
</description>

<category domain="https://awesome_domain>Awesome Category 1</category>

<category domain="https://awesome_domain”>Awesome Category 2</category>

<category domain="https://awesome_domain”>Awesome Category 3</category>

<category domain="https://awesome_domain”>Awesome Category 4</category>

<pubDate>Wed, 17 Sep 2014 17:57:50 GMT</pubDate>

<author>Awesome author</author>

<guid>Awesome GUID</guid>

<dc:date>Awesome Date</dc:date>

<clearspace:dateToText>3 hours, 35 minutes ago</clearspace:dateToText>

<clearspace:objectType>0</clearspace:objectType>

</item>

对于上面的内容,我创建了一个名为Item的示例JAVA类,看起来像这样

@Root
public class Item {

    @Element
    private String title;

    @Element
    private String link;

    @Element
    private String description;

    // @ElementMap(entry = "category", key = "domain", attribute = true, inline
    // = true)
    // private Map<String, String> map;

    @ElementList(entry = "category", inline = true, type = String.class)
    private List<String> categoryList;

    @Element
    private String pubDate;

    @Element
    private String author;

    @Element
    private String guid;

    @Element
    private String date;

    @Element
    private String dateToText;

    @Element
    private String objectType;
}

但是我收到以下错误

com.android.volley.VolleyError: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=category, inline=true, name=, required=true, type=class java.lang.String) on field 'categoryList' private java.util.List com.xx.xx.Item.categoryList for class com.xx.xx.xx.Item at line 210

我现在花了将近2个小时。任何人都知道我做错了什么?我也尝试过在评论中看到的ElementMap,但这也没有用。

更新
我还尝试创建一个单独的Category类,如下所示

@Root
public class Category {

    @Attribute
    protected String domain;

    @Text
    private String value;

}

在Item类中更改我的categoryList对象,如下所示

@ElementList(entry = "category", inline = true, type = Category.class)
private List<Category> categoryList;

但徒劳无功! :(

更新
即使这样也行不通!

@ElementList(inline=true)
private List<Category> categoryList;

0 个答案:

没有答案