Why does this loop only work once?

Why does this loop only work once? noteDatabaseItem accepts only node and populates data. xml has 3 notes in it.

XML:

<?xml version="1.0" encoding="utf-8"?>
<noteCollection>
  <note name="Test Note 1">This is test note 1 content!</note>
  <note name="Test Note 2">This is test note 2 content!</note>
  <note name="Test Note 3">This is test note 3 content!</note>
</noteCollection>

C ++:

std::vector<notekeeper::noteDatabaseItem> noteList;
TiXmlElement* noteCollection = xmlDoc->FirstChildElement("noteCollection");
TiXmlElement* node = noteCollection->FirstChildElement("note");
int itemCount = 0;

while (node != NULL) {
    itemCount++;
    noteList.resize(itemCount);
    noteList.push_back(noteDatabaseItem(node));
    node = noteCollection->NextSiblingElement("note");
}
+3
source share
3 answers

Shouldn't it be node = node->NextSiblingElement("note")?

noteCollection only has children, not siblings, right?

+9
source

You get the wrong item in your loop. Try the following:

while (node != NULL) {
    itemCount++;
    noteList.push_back(noteDatabaseItem(node));
    node = node->NextSiblingElement("note");
}

The next brother of the current node is the one you want. You tried to get the next brother of the parent node.

+3
source
node = noteCollection->NextSiblingElement("note");

node = node->NextSiblingElement("note");

. .

+1

Source: https://habr.com/ru/post/1768793/


All Articles