Search code examples
javaxmllwjglslick2d

Writing and reading from an XML file with Slick2D


I was wondering how you would go about writing and the reading from an .XML file using the Java library Slick2D.

I have googled for the last couple of days and have found nothing succinct and simple that explains what I'm looking for.

I am looking for 3 things:

  1. How to create an .XML file (would be awesome to be able to search a directory to see if there is an existing file and only create a new file if one does not already exist.
  2. How to write to the created .XML file.
  3. How to read from the found .XML file. The reason for this is that I'm trying to create a save/load class for a game.

Just a general snippet of code, that I'd be able to modify for my needs would be great even for a learning experience.

So now for my question, does anyone have any information that may help me on my quest to succeed in my above stated three goals?


Solution

  • For checking if a file exists you can use the exists() method of java.io.File. See this for more information.

    I don't know if slick2d provides it own xml utilities but you should always be able to use the standard java ways of reading/writing xml. You can use the DocumentBuilder like shown in this tutorial or a sax parser like described here. If you need to work with complex xml structures it might be worth to look into jaxb.