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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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");
}
How do I assign those values to variables? Is that what's happening on the level->Attribute("width",&width) line?
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