就您自己的东西,尤其是游戏而言,XML 被高估了。如果您在一家大公司工作,其他人需要查看您的文件的文本,那么 XML可能是有意义的,但即使在这种情况下,您也可能会编写一个可以为您完成所有工作的关卡编辑器。
我建议使用您自己的约定的简单文本文件,或者使用java.io.ObjectOutputStream
and java.io.ObjectInputStream
将数据直接写入数据文件(如果您有很多信息或对象)或java.util.Properties
写入数据(如果您只需要写原语)。
上面的很多人都提到 XML 有多好,因为它具有“自动序列化”,但我真的认为在 Java 中工作时不值得一提,因为我上面列出的两种方法也是自动的,但在更高级等级。
另外不要忘记,使用 XML 标记表示 2D 瓦片地图非常不直观,而在字符文件中逐行写入它们相当容易读写。
前任。)
WWWWWWWWWW
WSWWWWW EW
W W K WW W
W W WW W
W WW WWWDW
W X W
WWWWWWWWWW
你可以像上面那样做,哪里W
是墙,哪里是S
开始,哪里是E
出口,哪里K
是钥匙,哪里是D
门,哪里是X
怪物。一切都在哪里以及应该如何设置关卡非常明显,即使是文本文件也是如此。
我在许多专业游戏项目中使用过文本文件。事实上,我只在为某人完成合同工作时才使用 XML,因为 XML 看起来“更专业”,尽管在大多数情况下使用它是不必要的,而且只是浪费空间,使事情变得过于复杂。
另一个例子:
SpawnFrequency 0.1 20.5
HealthPoints 5
Stats 12 500 30 10 11
你可以想象任何其他可能发生的事情。然后,当您将文件读入 Java 时,只需使用BufferedReader.readLine()
获取每个参数,然后String.split()
获取该参数的所有值的数组。然后,比较拆分数组中的第一个值的大型 switch 语句或 if/else 分组将确定您实际读取的参数。当您达到这一点时,您就接近了 XML 的用处,但是对于这种用途,它仍然可能过于臃肿。然而,XML 可以帮助解释其中一些神秘的值可能是什么:
XML中的上述内容:
或者可能是这样的:
`<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>`
但是,当您再次开始遇到这种复杂性时,您不妨开始使用java.io.Serializable
它,因为它非常简单。
编辑
这是一个可序列化的示例。
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();
}
}
}
通常,您的文件将是这样的:new java.io.File("/Documents/MyGame/MyDataFile.whateverIFeelLikeAsLongAsItsConsistent")
.