Rapidxml: how to go through nodes? It turns out the last brother

Using quickxml, I want to iterate over a set of nodes and use what seems to me to be the best way to do this (from a reliable stackoverflow, the document does not seem to have an iteration example):

while (curNode->next_sibling() !=NULL ) { string shiftLength = curNode->first_attribute("shiftLength")->value(); cout << "Shift Length " << "\t" << shiftLength << endl; curNode = curNode->next_sibling(); } 

Unfortunately, on my OSX 10.6, this is leaving the last sibling node - I think because next_sibling is called twice in the last iteration of the loop. I can get this last node if I write after the loop:

 cout << " LAST IS: " << curNode->first_attribute("shiftLength")->value(); 

... but it's quirky, and the program terminates at this point.

First question: can this be a unique error of my installation (OSX 10.6) or am I encoded incorrectly?

Second question: Does anyone have an example of what they think is the right way to iterate through an unknown number of XML nodes using quickxml?

Thanks guys,

Pete

+7
source share
3 answers

This is the correct way to repeat all the child nodes of a node in quickxml:

 xml_node<> *node = ... for (xml_node<> *child = node->first_node(); child; child = child->next_sibling()) { // do stuff with child } 
+11
source

Here's the final code in working form:

 while( curNode != NULL ) { string start = curNode->first_attribute("start")->value(); string numStaff = curNode->first_attribute("numStaff")->value(); cout << start << "\t" << numStaff << endl; curNode = curNode->next_sibling(); } 
+5
source
 while (curNode->next_sibling() !=NULL ) 

This suggests that "there is one more node left after I work." That's why your loop stops earlier - when curNode is the final brother, its " next_sibling " will be NULL. This test should work better:

 while (curNode !=NULL ) 
+1
source

All Articles