Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  XML Level Data?  (Read 767 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Member





« Posted 2010-04-01 06:55:07 »

Hi guys,

I'm new here, so Hi, i'm Matt.

I've been developing games in XNA/C# for a few years now and finaly switched to Java.

My current project is a Space Empire-building/fleet combat RTS type of thing. Its currently at a very early stage. I have a basic engine that gets all my stars, planets, moons and other objects (ships, commets, asteroids, etc) on the screen and all orbiting around correctly.

What I have been thinking about at the moment is loading a solar system from a file (at the moment its all hard-coded).

Each level (solar system) is stored in a tree structure, eg:

1  
2  
3  
4  
5  
6  
Sun
->Planet 1
->Planet 2
  ->Planet 2, Moon 1
  ->Planet 2, Moon 2
->Planet 3


is it possible to store this in an XML file and load it while preserving the hirarchy?

I think I'd have to use recursion, but i'm not sure what the best way to go is.
Otherwise, is there a tree data structure in Java that is Serializable?

If anyone has any ideas or advice, that would be very much apreceated.

Thanks,
Matt Taylor
Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #1 - Posted 2010-04-01 09:32:04 »

Create your own data structure, eg:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
public class Planet {
   private String name;
   private ArrayList<Planet> orbits = new ArrayList();

   public Planet () {
   }

   public Planet (String name) {
      this.name = name;
   }

   public String getName () {
      return name;
   }

   public void setName (String name) {
      this.name = name;
   }

   public ArrayList<Planet> getOrbits () {
      return orbits;
   }

   public void setOrbits (ArrayList<Planet> orbits) {
      this.orbits = orbits;
   }
}


There are many ways to serialize this. Here is an easy to use, human readable and editable solution:
http://code.google.com/p/yamlbeans/
First you setup some config settings for how you want the output:

1  
2  
3  
YamlConfig config = new YamlConfig();
config.writeConfig.setWriteRootTags(false);
config.setPropertyElementType(Planet.class, "orbits", Planet.class);


You can read the javadocs to see what settings are available. This won't write a tag (aka class name) for the root element (since we know it will always be a Planet) and it won't write a tag for each element in the "orbits" list, since we tell it they are always Planets.

Now to serialize your objects:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
Planet sun = new Planet("Sun");
sun.getOrbits().add(new Planet("Planet 1"));
Planet planet2 = new Planet("Planet 2");
planet2.getOrbits().add(new Planet("Moon 1"));
planet2.getOrbits().add(new Planet("Moon 2"));
sun.getOrbits().add(planet2);
sun.getOrbits().add(new Planet("Planet 3"));

YamlWriter writer = new YamlWriter(new FileWriter("output.yaml"), config);
writer.write(sun);
writer.close();


This will output:

1  
2  
3  
4  
5  
6  
7  
8  
name: Sun
orbits:
-  name: Planet 1
-  name: Planet 2
   orbits:
   -  name: Moon 1
   -  name: Moon 2
-  name: Planet 3


Nice and clean! Note with YAML indentation is important (like Python) and it uses spaces, you can't use tabs. Finally,  to deserialize:

1  
2  
3  
YamlReader reader = new YamlReader(new FileReader("output.yaml"), config);
Planet planet = reader.read(Planet.class);
reader.close();

Offline Dreamcatchermatt

Junior Member





« Reply #2 - Posted 2010-04-01 11:03:56 »

Hi Nate,

Wow, thats way simpeler than i thought it would be Smiley

I'll have a go at implimenting it this evening and post how it goes Smiley

Many thanks!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #3 - Posted 2010-04-02 09:15:15 »

Just to say that is it really simple to deal with XML in java. I often use it myself. Just to give you an idea :

Writing :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
public static void save(Drawing drawing,OutputStream os)
          throws ParserConfigurationException,
                 TransformerConfigurationException,
                 TransformerException
  {
    DocumentBuilderFactory documentBuilderFactory =
                                   DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder =
                                   documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();


    Element rootElement = document.createElement("XXXX");

    rootElement.setAttribute("width", ""+drawing.getSize().x);
    rootElement.setAttribute("height", ""+drawing.getSize().y);
    rootElement.setAttribute("id", drawing.getId());

    document.appendChild(rootElement);

    ArrayList<DrawingElement> elements = drawing.getDrawingElement();

    for(int i=0;i<elements.size();i++)
    {
      DrawingElement e = elements.get(i);

      if (e.getDrawingElement() == null)
      {
        saveShape(document,e,rootElement);
      }
      else
      {
        saveGroup(document,e,rootElement);
      }
    }
   
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result =  new StreamResult(os);
    transformer.transform(source, result);
  }


Reading :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
public static Drawing load(InputStream is) throws ParserConfigurationException, SAXException, IOException
  {
    Drawing drawing = new Drawing();

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse (is);

    doc.getDocumentElement ().normalize ();

    Element docElement = doc.getDocumentElement();

    NodeList childs = doc.getChildNodes();

    for(int s=0; s<childs.getLength() ; s++)
    {
      Node child = childs.item(s);

      if(child.getNodeName().equals("XXXXX"))
      {
        loadDrawing((Element)child,drawing);
      }
      else
      {
        System.out.println("Root child : "+child.getNodeName());
      }
    }

    drawing.validate();

    return drawing;
  }


Take an look here, it is not bad  Wink
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #4 - Posted 2010-04-02 12:46:19 »

You could use XMLEncoder/XMLDecoder, it is a part of the standard Java API.

Offline Nate

JGO Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #5 - Posted 2010-04-03 01:12:30 »

Take an look here, it is not bad  Wink
I dunno, that API is pretty nasty. XOM has a nice XML API, and a good slideshow about why most XML APIs suck. But neither API will automatically serialize objects. To do that you'd want XStream, which actually has an API very similar to the YamlBeans code posted above (<if-you-like>typing things twice</if-you-like> for hand edits Wink).

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (131 views)
2013-05-15 15:01:13

theagentd (119 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.32 seconds with 21 queries.