Android上的简单XML - 类MyArrayList <e>扩展了ArrayList <e>?</e> </e>

时间:2011-10-20 11:04:06

标签: java android xml

我是简单的xml库的新手。我真的很喜欢它,但我遇到了问题。

以下是我的课程(已删除一些代码以使其更简洁):

@Root
@ElementList
public class MyArrayList<E> extends ArrayList<E>{

public void ToXml() throws Exception{
            Serializer serializer = new Persister();
            File file = new File("somewhere in my file system");
            serializer.write(this, file);
    }
}

¬

@Root
public abstract class MediaEntry implements Serializable {
            private static final long serialVersionUID = 1L;

            @Element
            public String Title;
            @Element
            public String Description;
            @Element
            public String Url;
            @Element
            public String LocalPath;

            public MediaEntry(String title, String description, 
                              String url, String localPath) {
                            Title= title;
                            Description= description;
                            Url= url;
                            LocalPath= localPath;
            }
}

¬

public class VideoEntry extends MediaEntry {
            private static final long serialVersionUID = 1L;

            public VideoEntry(String title, String description, 
                              String url, String localPath) {

            super(title, description, url, localPath);
            }
}

当我实例化MyArrayList时添加一些VideoEntries并调用ToXml,我只得到一个空根ie。

<MyArrayList />

如何解决这个问题?是否与MyArrayList是通用的事实有关?

1 个答案:

答案 0 :(得分:3)

List必须是元素的成员(并且没有单独的类)才能获得所需的行为,您可以将ElementList设置为内联,因此没有父元素。

@Root
public class MyArrayList<E> {   
    @ElementList(inline=true)
    ArrayList<E> list = new ArrayList<E>();

    public boolean add(E entry) {
        return list.add(entry);
    }

    public void ToXml() throws Exception {
        Serializer serializer = new Persister();
        File file = new File("somewhere in my file system");
        serializer.write(this, file);
    }
}

想到另一种可能更好的解决方案(你可以访问所有List功能 - 但我不确定是否有任何副作用,所以我保留原来的解决方案)

@Root
public class MyArrayList<E> extends ArrayList<E> {      
    @ElementList(inline=true)
    MyArrayList<E> list = this;

    public void ToXml() throws Exception {
        Serializer serializer = new Persister();
        File file = new File("somewhere in my file system");
        serializer.write(this, file);
    }
}

要反序列化,你必须为SimpleXML声明,哪个元素用于构造函数参数:

@Root
public abstract class MediaEntry implements Serializable {
    private static final long serialVersionUID = 1L;

    @Element
    public String Title;
    @Element
    public String Description;
    @Element
    public String Url;
    @Element
    public String LocalPath;

    public MediaEntry(@Element(name = "Title") String title,
            @Element(name = "Description") String description,
            @Element(name = "Url") String url,
            @Element(name = "LocalPath") String localPath) {
        Title = title;
        Description = description;
        Url = url;
        LocalPath = localPath;
    }
}

顺便说一下,如果你刚刚开始编程Java,你可能会考虑阅读Java代码约定 - 用大写字母启动方法和变量名称不是一个好习惯(这样你就可以防止习惯于坏习惯; - ))