简体   繁体   English

将数组列表另存为XML文件

[英]Saving an arraylist to a file as XML

Is it possible to simply dump an ArrayList to an XML file? 是否可以简单地将ArrayList转储到XML文件?

I have an array-list which contains various types of message objects. 我有一个包含各种消息对象类型的数组列表。

What I would like to do is save this to an XML file to keep a log. 我想做的是将其保存到XML文件中以保存日志。

What I have tried is this. 我试过的是这个。

public void saveConversation()
    {
        FileOutputStream f_out;
        try {
            f_out = new  FileOutputStream("convo.txt");
            // Write object with ObjectOutputStream
            ObjectOutputStream obj_out = new  ObjectOutputStream (f_out);
            // Write object out to disk
            obj_out.writeObject (convo);
        } catch (IOException ex) {
            Logger.getLogger(HunterCom.class.getName()).log(Level.SEVERE, null, ex);
        } 
    } 

This works but the file created doesn't make much seance. 这行得通,但是创建的文件并没有太多的吸引力。 I was wondering if there was a simple way to change this to readable XML? 我想知道是否有一种简单的方法可以将其更改为可读的XML?

看一下XStream ,它正是为完成此任务而设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM