Pet*_*233 7 c++ iterator rapidxml
使用rapidxml我想循环遍历一组节点,并且使用我发现的最好的方法(从可靠的stackoverflow,doc似乎没有迭代的例子):
while (curNode->next_sibling() !=NULL ) {
string shiftLength = curNode->first_attribute("shiftLength")->value();
cout << "Shift Length " << "\t" << shiftLength << endl;
curNode = curNode->next_sibling();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,在我的OSX 10.6 上,这就省去了最后一个兄弟节点 - 我想因为在循环的最后一次迭代中,next_sibling被调用了两次.如果我在循环之后写,我可以得到最后一个节点:
cout << " LAST IS: " << curNode->first_attribute("shiftLength")->value();
Run Code Online (Sandbox Code Playgroud)
......但那很狡猾,程序就此退出.
第一个问题:这可能是我设置的唯一缺陷(OSX 10.6)还是我编码错了?
第二个问题:有没有人有一个他们认为使用rapidxml迭代未知数量的XML节点的正确方法的例子?
多谢你们
皮特
kaa*_*lus 12
这是在rapidxml中迭代节点的所有子节点的正确方法:
xml_node<> *node = ...
for (xml_node<> *child = node->first_node(); child; child = child->next_sibling())
{
// do stuff with child
}
Run Code Online (Sandbox Code Playgroud)
这是工作形式的最终代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
while (curNode->next_sibling() !=NULL )
Run Code Online (Sandbox Code Playgroud)
这表示“在我正在处理的节点之后还剩下一个节点”。这就是为什么你的循环提前停止的原因——当curNode最后一个同级出现时,它的“ next_sibling”将为NULL。这个测试应该效果更好:
while (curNode !=NULL )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9941 次 |
| 最近记录: |