rapidxml: how to iterate through nodes? Leaves out last sibling

ε祈祈猫儿з 提交于 2019-12-01 16:56:30

This is the proper way to iterate though all child nodes of a node in rapidxml:

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

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();
}
while (curNode->next_sibling() !=NULL )

This says "while there's one more node left after the one I'm working on". That's why your loop's stopping early - when curNode is the final sibling, its "next_sibling" will be NULL. This test should work better:

while (curNode !=NULL )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!