Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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 1058 times)
0 Members and 1 Guest are viewing this topic.
Offline Dreamcatchermatt

Junior Duke





« Posted 2010-04-01 04: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 Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2010-04-01 07: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 Duke





« Reply #2 - Posted 2010-04-01 09: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!
Legends of Yore - The Casual Retro Roguelike
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #3 - Posted 2010-04-02 07: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
« Reply #4 - Posted 2010-04-02 10:46:19 »

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

Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #5 - Posted 2010-04-02 23: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.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!