3

Why is this loop only running once? noteDatabaseItem just takes a node and fills in the data. the 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");
}
casablanca
  • 69,683
  • 7
  • 133
  • 150
Will03uk
  • 3,346
  • 8
  • 34
  • 40

3 Answers3

9

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

noteCollection has only children, not siblings, right?

GôTô
  • 7,974
  • 3
  • 32
  • 43
3

You're getting the wrong element in your loop. Try this:

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

The next sibling of the current node is the one you want. You were trying to get the next sibling of the parent node.

JoshD
  • 12,490
  • 3
  • 42
  • 53
1
node = noteCollection->NextSiblingElement("note");

is meant to be

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

Stupid mistake. Sibling not Child.

Will03uk
  • 3,346
  • 8
  • 34
  • 40