使用xstream的XmlArrayList和FilePersistenceStrategy如何控制输出xml文件的名称?

时间:2013-04-17 16:55:10

标签: java xstream

使用xstream的XmlArrayList和FilePersistenceStrategy如何控制输出xml文件的名称?

目前文件名为:int@0.xml

我需要用商业上可理解的文件/

来命名它们

link:http://x-stream.github.io/index.html

代码: PersistenceStrategy strategy = new FilePersistenceStrategy(new File(“C:\ workingTemp”));

列表清单=新的XmlArrayList(策略);

list.add(MyObj中)

由于

1 个答案:

答案 0 :(得分:0)

我不确定,如果这是最好的选择,但我认为它会起作用。

通过扩展CustomeFilePersistenceStrategy并覆盖FilePersistenceStrategy方法创建自定义策略类getName,如下所示:

public class CustomeFilePersistenceStrategy extends FilePersistenceStrategy {
     public CustomeFilePersistenceStrategy(File baseDirectory) {
      super(baseDirectory);
     }

    @Override
    protected String getName(final Object key){
        //put desired file naming logic
        return "CustomFile.xml";
    }
}

然后用法可能是:

 PersistenceStrategy strategy = 
               new CustomeFilePersistenceStrategy(new File("C:\workingTemp"));

 List list = new XmlArrayList(strategy);

哪个应该在输出文件夹中生成CustomFile.xml