Search code examples
javaxstream

Using xstream's XmlArrayList and FilePersistenceStrategy how can I control the name of the output xml file?


using xstream's XmlArrayList and FilePersistenceStrategy how can I control the name of the output xml file?

Currently the files are named : int@0.xml

I need to name them with a business understandable file/

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

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

List list = new XmlArrayList(strategy);

list.add(myObj)

Thanks


Solution

  • I am not sure, if this is the best option, but I think it will work.

    Create a custom strategy class CustomeFilePersistenceStrategy by extending FilePersistenceStrategy and override getName method as below:

    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";
        }
    }
    

    Then the usage could be:

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

    Which should generate CustomFile.xml in the output folder.