Loading and saving block – based games in Java XML or txt?
I have been making a sim game based on 2D tiles for some time, and everything is going well! Thank you for this website and kind people (you!), I just finished the pathfinding part of the game. That's great! thank you! In any case, for this problem
At present, the test level is hard coded into the game Obviously I need to do it again My idea is to save all the variables in my game class to a text file in various ways I can also write the details of each level to the file to load My question is, should I use only text files or XML? I understand what XML is basically, but I really don't know how to use it with Java, or why it is better than plain text files I have a little Google, an encyclopedia about XML and java! I don't see how I need to know how to use XML and Java to do this, but I do need to know more than I do
Thank you for your help
Editor: I'll try a few ideas here until I find the one that works best, and then choose it as the answer
Edit: select xStream Easy to use!
Solution
If you have game status in the object (such as HashMap or your own designed object), you can use xStream to write it as an XML file, and then use xStream to read it back to the new object
There is a two minute tutorial that shows you how to serialize / deserialize It can handle most objects without difficulty - you don't need to implement or specify any specific interface You don't need jaxp, sax, DOM, etc