我对简单的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是泛型有关吗?

最佳答案

列表必须是元素的成员(并且没有独立的类)才能获得所需的行为,您可以将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);
    }
}

只是想到了另一个可能更好的解决方案(你可以访问所有列表函数-但我不确定是否有任何副作用,所以我保留原来的解决方案)
@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声明,该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代码约定——以大写字母开始方法和变量名称并不是一个好的做法(这样您就可以防止习惯坏习惯;—)

10-07 12:58