In terms of your own stuff, especially games, XML is overrated. If you were working in a large company where other people need to view the text of your files, XML might make sense, but even in that case you would probably have a level editor written that would do everything for you.
I would recommend either having a simple text file with your own convention, or using java.io.ObjectOutputStream
and java.io.ObjectInputStream
to just write your data directly to a data file (if you have a lot of information or objects) or java.util.Properties
to write your data (if you only need to write primitives).
A lot of the people above reference how XML is nice because it has "automatic serialization," but I really don't think that's worth mentioning when working in Java, because both methods I listed above are also automatic, but on a much more advanced level.
Also don't forget that representing a 2D tile map using XML tags is very unintuitive, whereas just writing them line by line in a character file is fairly easy to read and write.
ex.)
WWWWWWWWWW
WSWWWWW EW
W W K WW W
W W WW W
W WW WWWDW
W X W
WWWWWWWWWW
You could do something like the above, where W
is a Wall, S
is Start, E
is Exit, K
is Key, D
is Door, and X
is a monster. Exactly where everything lies and how the level should be set out is very obvious, even as a text file.
I've used text files among plenty of professional game projects. In fact, I've only used XML when I've done contracting work for somebody, because XML looks "more professional" even though in the majority of cases it's used it is unnecessary and just a waste of space that overcomplicates things.
Another example:
SpawnFrequency 0.1 20.5
HealthPoints 5
Stats 12 500 30 10 11
You can imagine whatever else might happen. Then as you read the file into Java, simply use BufferedReader.readLine()
to get each parameter, then String.split()
to get an array of all the values for that parameter. Then a large switch statement or if/else grouping comparing the first value in the split array will determine which parameter you've actually read. When you get to this point, you're approaching XML's usefulness, but it still might be overly bloated for this use. XML, however, could help to explain what some of those mysterious values could be:
The above in XML:
Or maybe like this:
`<GuyStuff>`
`<SpawnFrequencyMinimum>0.1</SpawnFrequencyMinimum>`
`<SpawnFrequencyMaximum>20.5</SpawnFrequencyMaximum>`
`<Health>5</Health>`
`<Strength>12</Strength>`
`<Agility>500</Agility>`
`<Stamina>30</Stamina>`
`<TechnoWizardry>10</TechnoWizardry>`
`<JavaSkillz>11</JavaSkillz>`
`</GuyStuff>`
But once again when you start getting to that complexity, you might as well just start using java.io.Serializable
because it's very easy.
EDIT
Here's a Serializable example.
public class MySaveData implements java.io.Serializable //Serializable doesn't need any methods implemented, it simply says, "hey Java, my object can be written to a file."
{
float data1;
int data2;
Object data3;
//BufferedImage data4; //Note that in order for an object to serialize, it can't contain any data members that are not Serializable, so this would be illegal.
}
import java.io.*;
public class DataReader
{
public void saveData(File loc, MySaveData obj1, MySaveData obj2, MySaveData obj3)
{
try
{
//You can use any type of OutputStream to create an OOS, so you can for
//example use one when you're connected with Sockets or something similar.
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(loc));
//Writing objects is tremendously easy, and so are primitives.
//Note that the order you write them in is the same order you will read them.
os.writeObject(obj1);
os.writeObject(obj2);
os.writeObject(obj3);
os.writeDouble(someValueINeededToWrite);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void loadData(File loc, PlaceToPutData place)
{
try
{
//ObjectInputStream follows the same rules as OOS mentioned above.
ObjectInputStream is = new ObjectInputStream(new FileInputStream(loc));
//Remember to read your data back the same order you wrote it.
place.obj1 = is.readObject();
place.obj2 = is.readObject();
//If you don't want one of the objects for some reason and want
//to get something after it, you can just read the next one but not use it.
readObject();
place.value = is.readDouble();
is.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Typically your File would be something like this: new java.io.File("/Documents/MyGame/MyDataFile.whateverIFeelLikeAsLongAsItsConsistent")
.