My Java application's save files are in XML, and in certain cases, a user might want to merge two save files together. 开发者_StackOverflow中文版 What packages have people used to do this? Any caveats to offer?
This question is a little broad given we don't know the size of the files, what kind of data they store or what kind of data conflicts there could be between two files.
Given two files with no potentially conflicting items and no ordering requirements, you can load them both up as DOM documents, create a third output DOM document and import the children from the two source files into the root element of the third, before saving it.
For larger files this might be memory intensive, so you might want to use SAX or STAX processing (which is basically stream processing for XML).
If you can provide more details people can give you a better answer.
"Merge" can be interpreted in many different ways and depends on the semantics of the data being merged. For instance, when merging two items do you choose one over the other or combine their attributes and contained items? Any intelligent solution will have to answer that question, and others, for each type of object and sub-object being merged.
精彩评论