views:

84

answers:

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");
}
+6  A: 

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

noteCollection has only children, not siblings, right?

GôTô
+1  A: 

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
A: 
node = noteCollection->NextSiblingElement("note");

is meant to be

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

Stupid mistake. Sibling not Child.

Will03uk
You should accept GoTo's answer, not re-post it. (i.e., delete this answer.)
GMan