Menu

Parsing Map Data!

Developer
2004-01-03
2004-01-03
  • The Frozen Angels Team

    Please there's no cool tutorial on TinyXML, out there. it's really cool parser but please write 4 or 5 tutorials showing how to parse some examples.

    Can any one show me how to parse this XML:

    <?xml version="1.0" standalone="no" ?>
    <!-- Our Map data -->
    <Map Name="Horror Dungeon" Height="20" Width="20">
        <TileSet Name="Dungeon">
        <Tile X="0" Y="0" Index="1"/>
        <Tile X="32" Y="0" Index="4"/>
        <Tile X="64" Y="0" Index="2"/>
        <Tile X="96" Y="0" Index="9"/>
        <Tile X="128" Y="0" Index="4"/>
        </TileSet>
    </Map>

    Main thing i want to know is how to get and save attributes in a string, plus how to print all this out by reading,
    i mean how can i print like this:

    Map Name: Horror Dungeon
    Map Dimensions: 20x20
    TileSet Name: Dungeon
    Tile1 XPOS:0 YPOS:0 Index:1
    etc....

     
    • B Sizer

      B Sizer - 2004-01-03

      TinyXML isn't that hard; you certainly don't need 4 or 5 tutorials! The beauty of XML parsing is that there are only really ever 2 things you need to do:

      1) find nodes
      2) read a node's attributes (or text)

      Firstly, make sure you read docs/index.html in the TinyXML package. That has an example not too dissimilar to yours that shows you how the node tree is laid out. With that in mind, you can write code to start accessing it.

      Step 1: create the document and load in the data.

      TiXmlDocument doc;
      ifstream istr("filename.xml");
      istr >> doc;

      Step 2: find the element that's important to you.

      TiXmlElement* map = doc.RootElement(); // should be <Map>

      Step 3: show any attributes you like

      cout << "Map name: " << map->Attribute("Name") << endl;
      int x,y;
      map->Attribute("Height", &y);
      map->Attribute("Width", &x);
      cout << "Map dimensions: " << x << "x" << y << endl;

      Step 4: Find important child node

      TiXmlElement* tileset = map->FirstChildElement("TileSet");
      // show whatever attributes you like

      Step 5: iterate through multiple nodes
      TiXmlElement* tile = tileset->FirstChildElement("Tile");
      while (tile)
      {
          int tx, ty;
          tile->Attribute("X", &tx);
          tile->Attribute("Y", &ty);
          cout << "TileX: " << tx << " TileY: " << ty << endl; // etc...

          // Look for the next one
          tile = tile->NextSiblingElement("ENTRY");
      }

      Pretty much everything you'll ever need is shown in that example above (assuming I remembered it all correctly). All your tree navigation functions are in TiXmlNode (docs/classTiXmlNode.html). Most of the querying functions you need are in TiXmlElement (docs/classTiXmlElement.html).

       

Log in to post a comment.

MongoDB Logo MongoDB