rapidxml:如何遍历节点?留下最后的兄弟姐妹 [英] rapidxml: how to iterate through nodes? Leaves out last sibling

查看:539
本文介绍了rapidxml:如何遍历节点?留下最后的兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用rapidxml我想循环一组节点,并且使用我发现的最好的方法(从可靠的stackoverflow,doc似乎没有迭代的例子):

Using rapidxml I'm wanting to loop through a set of nodes, and am using what I found to be the best way to do this (from trusty stackoverflow, the doc doesn't appear to have an example of iteration):

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

不幸的是,在我的OSX 10.6上,这是遗漏了最后一个兄弟node - 我猜是因为在循环的最后一次迭代中,next_sibling被调用两次。如果我在循环之后写,我可以到达最后一个节点:

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

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

...但这很狡猾,程序在此时退出。

...but that's dodgy, and the program quits at that point.

第一个问题:这可能是我设置的唯一缺陷(OSX 10.6)还是编码错误?

First question: Could this be a unique foible of my setup (OSX 10.6) or have I coded wrong?

第二个问题:是吗任何人都有一个他们认为使用rapidxml迭代未知数量的XML节点的正确方法的例子吗?

Second question: Does anyone have an example of what they believe is the correct way to iterate through an unknown number of XML nodes using rapidxml?

谢谢你们

Pete

推荐答案

这是在rapidxml中迭代节点的所有子节点的正确方法: / p>

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
}

这篇关于rapidxml:如何遍历节点?留下最后的兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆