I'm trying to implement a level loader for JME, part of which is responsible for loading the terrain. The level definition is provided in XML (of course!). My XML is:
This is a quick question - am I right in thinking that detail is a child node of terrain and the three filenames are child nodes of detail? This is the document model.
I tried to use getChildNodes() on the terrain Node, but I get three nodes in my NodeList that are junk. Am I doing something silly?
elements = document.getElementsByTagName("terrain");
Node heightmap = elements.item(0);
NodeList info = heightmap.getChildNodes();
System.out.println(info.getLength());
for (int ii=0;ii<info.getLength();ii++)
{
Node item = info.item(ii);
System.out.println(item.getNodeName() + " " + item.getNodeValue());
}
The xml is well formed, otherwise the parser would complain.
Your output is correct: you are listing children of the <terrain> node - these are: one Text with a line feed, one Element named detail, another Text with a line feed.
If you want to load a level, which might be large, from xml I would consider using SAX not DOM because DOM needs alot of memory (as it holds the entire xml file as unicode - plain file size doubled - in heap memory),
I originally started with SAX but I didn't like how it works - where you implement handler for the start and end of elements. You get told of the start of the element but then what? I'm still learning this through tutorials, so my opinion may change once I fully understand how it works.
In my opinion the tag names should never get any indices (think of the DTD for that!). But what about the second without the numbering? Or if any numbers using attributes for them?
if you want to concentrate on the rest of your work instead of concentrating on the xml you might want to take a look at this: http://xstream.codehaus.org/tutorial.html
Yeah, I remember them talking about using it to create levels in it and be able to load them directly into your game. You might take a look at that instead of re-inventing the wheel. Unless of course you believe you can make one more round than theirs.
It's not that I think I can, it's just that it will help me learn more. It's the best way to learn Java, Swing (editor) and XML. Also, it might be more specific to my needs. But overall, it's a better learning process, which right now is more important than the end result.