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?

  • 2
    \$\begingroup\$ As far as I can see, this is unrelated to Slick2D or lwjgl. You seem to be asking "how to handle XML files in Java", which is a pretty easy question to Google. \$\endgroup\$ – Liosan Apr 19 '13 at 13:19
  • \$\begingroup\$ A easy to Google question that displays no easy answer, that's why I asked this question, because I wanted an easy to understand yet succinct way of understanding the subject. It is related to Slick2D and LWJGL in the sense that they are the library's that I am using. \$\endgroup\$ – GeorgeV Apr 19 '13 at 13:26
  • 3
    \$\begingroup\$ You're asking how to do X with tools that were made to do Y. Use different tools. Search for Java libraries made for reading and writing XML. KXmlParser is a decent one. Your first question is about file IO and the other two should be covered in the documentation for whatever XML library you decide to use. \$\endgroup\$ – MichaelHouse Apr 19 '13 at 15:27

Check out XStream, I found it to be extremely easy to use for serializing/de-serializing Java Objects.

For working with files such as checking if they exist, use the Java API: java.io.File is a simple way to check such a thing.

I think you should familiarize yourself with basic Java input/output before chasing goals such as serialization with XML.


Not the answer you're looking for? Browse other questions tagged or ask your own question.