如何获得对象列表的dot类?

时间:2015-03-27 14:17:52

标签: java android simple-framework

我想用SimpleFramework xml序列化一个Object对象。 我使用普通类成功但不使用对象列表。

我没有找到使用List of object执行此操作的良好语法。

List< Shop > shop = new Persister().read(List<Shop>.class, data);

List< Shop >.class无法正常工作

由于

1 个答案:

答案 0 :(得分:1)

不可能直接这样做;请改用@ElementList

以下是一个例子:

商店类

@Default // Or customize as you need
public class Shop
{
    private String name;

    public Shop(String name)
    {
        this.name = name;
    }

    private Shop() { /* Required default ctor */ }

    // ...
}

ListExample

这只是列表的一个包装。

@Root(name = "example")
public static class ListExample
{
    @ElementList(name = "Shops", inline = true)
    private List<Shop> shops;

    // ...
}

用法

String input = ... // Or what source you have
Serializer ser = new Persister();
ListExample readExample = ser.read(ListExample.class, input);