Using TinyXML to read an xml file. I want to loop over multiple elements but for some reason it only loops over the first element. A short example xml file would be:
<root>
<wave_manager name="WaveManager01">
</wave_manager>
<wave_manager name="WaveManager02">
</wave_manager>
</root>
The code to loop is:
TiXmlElement* root = _waveDoc.FirstChildElement("root");
for(TiXmlElement* e = root->FirstChildElement("wave_manager"); e != NULL; e = root->NextSiblingElement("wave_manager"))
{
string wmName = e->Attribute("name");
}
So this only loops once and wmName does show WaveManager01, but then the for loop exists.
Your for
statement is resetting the e
element to root on every iteration. It is supposed to go to the next element in list - e
.
Here is the correct code:
TiXmlElement* root = _waveDoc.FirstChildElement("root");
for(TiXmlElement* e = root->FirstChildElement("wave_manager"); e != NULL; e = e->NextSiblingElement("wave_manager"))
{
string wmName = e->Attribute("name");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With