Menu

New To XML

simreality
2004-10-16
2013-05-20
  • simreality

    simreality - 2004-10-16

    I've been looking through TinyXML for a few days but can't figure it out. I have already created a small XML file but cannot figure out the correct way to use it. Here's my XML:
    <level number="1" width="50" height="20">
        <block x="0" y="3" z="0" w="50" h="1"/>
        <block x="7" y="2" z="0" w="4" h="1"/>
        <block x="10" y="3" z="0" w="4" h="1"/>
        <block x="29" y="3" z="0" w="50" h="1"/>
        <block x="33" y="7" z="0" w="50" h="1"/>
        <block x="31" y="0" z="0" w="50" h="1"/>
        <block x="22" y="0" z="0" w="50" h="1"/>
        <block x="42" y="0" z="0" w="50" h="1"/>
        <block x="50" y="0" z="0" w="50" h="20"/>
        <block x="0" y="0" z="0" w="50" h="20"/>
        <player x="2" y="2" z="0" w="1" h="2"/>
    </level>

    Quite simple enough. How can I easily access those numbers? All I need to do is assaign those values to a bunch of variables in the code? I also need to have a function that counts the number of <block> lines so as to allocate the proper number of blocks in my program.
    Any help is appreciated.

     
    • Yves Berquin

      Yves Berquin - 2004-10-16

      Here's a code that read the file, count the <block> elements and allocate the width and height
      There's no error checking though

         TiXmlDocument simpledoc;
         TiXmlElement * level, * block;
         int height, nb_block, width;

         simpledoc . LoadFile ("simple.xml");
         level = simpledoc . RootElement ();
         level -> Attribute ("width", & width);
         level -> Attribute ("height", & height);
         nb_block = 0;
         block = level -> FirstChildElement ("block");
         while (block)
         {
            nb_block++;
            block = block -> NextSiblingElement ("block");
         }

       
    • simreality

      simreality - 2004-10-16

      How do I assign those values to variables? Is that what's happening on the level->Attribute("width",&width) line?

       
      • Yves Berquin

        Yves Berquin - 2004-10-17

        Yes, indeed,

        Take a look at the TiXmlElement documentation :

        http://www.grinninglizard.com/tinyxmldocs/classTiXmlElement.html

        It explains the 3 different forms you can use to retrieve the attribute values.
        You can also use TiXmlElement::QueryIntAttribute

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.