如何循环 n 次,在 xml 文件中每个循环一个级别
How to loop n times, one level per loop in an xml file
我觉得这应该很容易,但几个小时后我仍然无法弄清楚。我尝试在谷歌上搜索它,但似乎我的大脑想要问这个问题的唯一方法是 3 行解释,这与谷歌并不真正有效。如果您有更好的表达方式,请随意编辑。
我有一个 xml 文件,这样说:
<tag1>
<tag2>
<tag3>
...
</tag3>
</tag2>
</tag1>
文档的每个父级可能有许多标签 1、标签 2 和标签 3。
使用 c++ 的 pugixml 时,我想在所选节点上执行操作。这是我想完成的伪代码,但以一种我知道是错误且不可行的方式。
for(pugi::xml_node tag1 : doc->child("tag1").children()){
//Do something with tag1
for(pugi::xml_node tag2 : doc->child("tag1").child("tag2").children()){
//Do something with tag2
for(pugi::xml_node tag3 : doc->child("tag1").child("tag2").child("tag3").children()){
//Do something with tag3
}
}
}
只是看着这个,很容易找到什么不起作用......我需要能够与doc.child((.child((.child((.child((.child((..child((.-循环内。必须为每次迭代添加 .child(( 会阻止我执行递归样式的操作,例如:
void loopXNestedTimes(int n){
if(n==0) return;
// Do my stuff
loopXNestedTimes(n-1);
}
知道我会怎么做吗?我正在使用Qt和c ++,但仍在学习两者,因此可能缺少一些语言功能可以做到这一点。
使用tag1
获取tag2
元素(而不是doc
(,使用tag2
获取tag3
元素,我认为这是您缺少的关键点。
您的代码片段应如下所示:
for (pugi::xml_node tag1 : doc->child("tag1").children()){
//Do something with tag1
for (pugi::xml_node tag2 : tag1.child("tag2").children()){
//Do something with tag2
for (pugi::xml_node tag3 : tag2.child("tag3").children()){
//Do something with tag3
}
}
}
相关文章:
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- C++-字符串是否包含一个带有简单循环的单词
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 使用一个循环与两个循环
- 用于在 C++ 中使用 while 循环查找下一个素数的简单函数
- 使用一个 for 循环如何迭代和测试迭代器值?
- 我正在尝试创建一个菜单,但我的代码一直在循环
- 转到基于范围的 for 循环中的下一个迭代器
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 如何循环 n 次,在 xml 文件中每个循环一个级别
- 初始化时 num 的哪个值会给这个嵌套循环一个"true"输出?
- 两次相同的 for 循环:一个编译,另一个不编译
- 如何在C 中循环一个语句
- 循环一个级数
- 如何在c++中循环一个函数
- 循环一个精灵以显示不止一次
- C++循环一个菜单,直到选择了所有选项
- QT:如何每秒循环一个方法?C++
- 循环一个mpl::map