XML Model Serialization?

Has anyone written code to save/load models to/from XML? I'm going to try writing something like this, and just want to make sure I'm not reinventing the wheel.
pherver has wipped up something VERY basic for our tank game. It doesn't load a lot of different kinds of things, but it's a start.

I attached one of our level xml files (a few objects are commented out while I change from pixel units back to meters)

Here's partial code we use to load it. It won't all make sense due to some stuff for our game framework, but the parsing should get you started

Code: Select all

            mainXML = new XML(loader.data)
            
            var worldAABB:b2AABB = new b2AABB();
            worldAABB.minVertex.Set(mainXML.world.@minX, mainXML.world.@minY);
            worldAABB.maxVertex.Set(mainXML.world.@maxX, mainXML.world.@maxY);
            
            // Define the gravity vector
            var gravity:b2Vec2 = new b2Vec2(mainXML.world.gravity.@x, mainXML.world.gravity.@y);
            worldLocator.defaultWorld = new World(worldAABB, gravity, true);

            // Add player
            var tank:Tank = new Tank(new b2Vec2(mainXML.world.player.@x, mainXML.world.player.@y));
            objectLocator.addDisplayableObject(tank);
            objectLocator.player = tank;
            
            // Load terrain
            
            var bd:b2BodyDef;
            var body:b2Body;
            var bbr:BaseBodyRenderer;
            for each (var object:XML in mainXML.world.terrain.children())
            {
               if(object.name() == "poly")
               {
                  bd = new b2BodyDef();
                  bd.position.Set(object.@x, object.@y);
                  bd.rotation = object.@rotation * Math.PI / 180;
                  
                  var pd:b2PolyDef = new b2PolyDef();
                  pd.vertexCount = object.children().length();
                  pd.friction = object.@friction;
                  pd.restitution = object.@restitution;
                  
                  for(var i:int = 0;i<object.children().length(); i++)
                  {
                     pd.vertices[i].Set(object.children()[i].@x, object.children()[i].@y);
                  }
                  

                  bd.AddShape(pd);
                  body = worldLocator.defaultWorld.CreateBody(bd);
                  bbr = new BaseBodyRenderer(body);

               }
               if(object.name() == "box")
               {
                  bd = new b2BodyDef();
                  bd.position.Set(object.@x, object.@y);
                  bd.rotation = object.@rotation * Math.PI / 180;
                  
                  var boxDef:b2BoxDef = new b2BoxDef();
                  boxDef.extents.Set(object.@width, object.@height);
                  boxDef.friction = object.@friction;
                  boxDef.restitution = object.@restitution;
                  bd.AddShape(boxDef);
                  
                  body = worldLocator.defaultWorld.CreateBody(bd);
                  bbr = new BaseBodyRenderer(body);
               }
            }
            for each (var object:XML in mainXML.world.objects.children())
            {
               if(object.name() == "ragdoll")
               {
                  objectLocator.addDisplayableObject(new RagDoll(new b2Vec2(object.@x, object.@y)));
               }
            }
            var levelLoaded:Event = new Event(Controller.LEVEL_LOADED);
            ced.dispatchEvent(levelLoaded);
Awesome, thanks! I'm pretty new to AS3 and it's standard library, so this is a huge help!

Original thread: https://box2d.org/forum/viewtopic.php?t=223