[Tutorial 6] libGDX –Serialize and deserialize Entitys with SimpleXML

Object Mapping with SimpleXML:

First of all, sorry for that long delay, but i was really busy.

After we declared the ComponentInterface and the ContainerClass, i will show you how i managed the serialization. The first thing i considered was using JAXB, but its not part of the Anrdoid Java Library and its about 9MB. Wasting 9 MB to XMLSerialisation is too much, so i took SimpleXML(400KB) (http://simple.sourceforge.net/). Tutorials can be found there. Too add SimpleXML to libGDX project, you can define a maven dependency or just use the jars, put them on a libs folder in your root libgdx project and add „compile fileTree(dir: ‚../libs‘, include: ‚*.jar‘)“ to your core  project dependencies in the build.gradle file. (see here for more details)

To keep it simple we will use a level format which consists of only Entitys, no templates or something like that. You could add more Entities but for space reason I show only one. XML it looks like:

No we want to Map our XML to the Java Object, in SimpleXML we can use Annotations to achieve that.

  • @Root Element declares the Root node of an XML file
  • @ElementList declares a List of Elements
  • @Element is a complex type e.g our ComponentInterface
  • @Attribute can access primitive members via getter/setter or by declaring it directly over the member declaration

So no we defined our Entity-XML-Mapping, but we also have a <level> tag defined, in which our entities can be found. To enable SimpleXML parsing the entity data, we need to define the level element. Its a simple container class.

  •  „inline = true“ Attribute defines that the elements are not contained in another tag, like components did
  • „required=false“ can be used for make attributes optional

Serialize and Deserialize it!

Now we lets try, if it works. We use a LevelFactory to get all entities parsed from a level xml.


Next time i will show you how to add our actors to the stage and implement a RenderComponent, which renders static pictures and animations.


Related Posts

Leave a reply