I'm programming a simple Java game and I wanted to save to player data to an XML encoded save file. Currently I'm discovering the Java XML DOM API and I have a short question.
Suppose I have the following XML file (doesn't make sense I know, but it is just an example)
<?xml version="1.0"?>
<savedGame>
<name></name>
<player>
<name>YOU</name>
<map>
<health>2000</health>
<name>map_test.map</name>
<position>
<x>0</x>
<y>0</y>
</position>
</map>
<stats>
<health>1000</health>
<xp>
<melee></melee>
<magic></magic>
<ranged></ranged>
<agility></agility>
</xp>
</stats>
<items>
<item id="0">
<amount></amount>
</item>
</items>
</player>
</savedGame>
Now I want to get the players health level (note that there is an other health element in the map section). This can be done with the following code:
String hp = ((Element) ((Element) savedGameDocument.getElementsByTagName("player").item(0)).getElementsByTagName("stats").item(0)).getElementsByTagName("health").item(0).getTextContent()
It works, yes. But it looks very nasty to me. Is this the usual way for getting those nested elements? And why isn't there an ElementList class? Now I have to cast every Node.
Thanks in advance, Jori.
javax.xml.bindpackage).