Problem with loading XML file using DOM (implementing level loader for JME)

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:


<root>
<terrain heightmap="/opt/media/heightmap.jpg" scale="0.1, 0.1, 0.1" position="0, 0, 0">
   <detail>
      <filename>"/opt/media/detail1.jpg"</filename>
      <filename>"/opt/media/detail2.jpg"</filename>
      <filename>"/opt/media/detail3.jpg"</filename>
   </detail>
</terrain>
</root>



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 two printlns give:

3
#text
       
detail null
#text



Thanks

It's probably not well formed XML.  Have you tried taking your xml and opening it in IE or Firefox as a sanity check?

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.

Have a look at the JavaDoc of Node.



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.



Thanks

What feels more natural to you:


<root>
<terrain heightmap="/opt/media/heightmap.jpg" scale="0.1, 0.1, 0.1" position="0, 0, 0">
   <detail>
      <filename>"/opt/media/detail1.jpg"</filename>
      <filename>"/opt/media/detail2.jpg"</filename>
      <filename>"/opt/media/detail3.jpg"</filename>
   </detail>
</terrain>
</root>



Or:

<root>
<terrain heightmap="/opt/media/heightmap.jpg" scale="0.1, 0.1, 0.1" position="0, 0, 0">
   <detail1 filename="/opt/media/detail1.jpg">
   <detail2 filename="/opt/media/detail2.jpg">
   <detail3 filename="/opt/media/detail3.jpg">
</terrain>
</root>

the second IMHO.  Make sure you add / at the end of those detailX tags.

Oops, yes - I forgot those. Thanks!

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

Interesting stuff - thanks!



I'm interested in learning XML as I'm trying to learn Java and its technologies. This may or may not improve my career options. :slight_smile:

Getting there one step at a time:


Isn't there already a level loader in jME or is that something with the level-builder thing?



darkfrog

darkfrog said:

Isn't there already a level loader in jME or is that something with the level-builder thing?

darkfrog


I don't know. Anyone else know?

MonkeyWorld3D surely has one…

That's the one! :-p



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. :wink:



darkfrog

chuckle



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.



Thanks  :smiley:

Hi there stodge.



Well, we do almost the same in MonkeyWorld3D. We generate the terrain in runtime or even paint the terrain

heightmap in runtime. Then when we save the terrain, it will form part of the level xml file. Also the heightmap

that was generated will be saved as a .png file.



This is the basic format of the terrain: