Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyXML looping over elements

Tags:

c++

xml

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.

like image 306
user441521 Avatar asked Aug 27 '12 22:08

user441521


1 Answers

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");

}
like image 63
Mendes Avatar answered Sep 29 '22 03:09

Mendes