使用rapidxml在节点中循环
Looping through a node using rapidxml
我刚开始在C++中使用XML,我想循环遍历一个XML节点,并将的"id"属性打印到向量中。这是我的XML
<?xml version="1.0" encoding="UTF-8"?>
<player playerID="0">
<frames>
<frame id="0"></frame>
<frame id="1"></frame>
<frame id="2"></frame>
<frame id="3"></frame>
<frame id="4"></frame>
<frame id="5"></frame>
</frames>
</player>
这就是我加载XML 的方式
rapidxml::xml_document<> xmlDoc;
/* "Read file into vector<char>"*/
std::vector<char> buffer((std::istreambuf_iterator<char>(xmlFile)), std::istreambuf_iterator<char>( ));
buffer.push_back(' ');
xmlDoc.parse<0>(&buffer[0]);
如何循环通过节点?
一旦将xml加载到文档对象中,就可以使用first_node()
获取指定的子节点(或仅获取第一个子节点);则可以使用CCD_ 2遍历其所有同级。使用first_attribute()
获取节点的指定(或仅第一个)属性。这是一个代码外观的想法:
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <rapidxml.hpp>
using std::cout;
using std::endl;
using std::ifstream;
using std::vector;
using std::stringstream;
using namespace rapidxml;
int main()
{
ifstream in("test.xml");
xml_document<> doc;
std::vector<char> buffer((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>( ));
buffer.push_back(' ');
doc.parse<0>(&buffer[0]);
vector<int> vecID;
// get document's first node - 'player' node
// get player's first child - 'frames' node
// get frames' first child - first 'frame' node
xml_node<>* nodeFrame = doc.first_node()->first_node()->first_node();
while(nodeFrame)
{
stringstream ss;
ss << nodeFrame->first_attribute("id")->value();
int nID;
ss >> nID;
vecID.push_back(nID);
nodeFrame = nodeFrame->next_sibling();
}
vector<int>::const_iterator it = vecID.begin();
for(; it != vecID.end(); it++)
{
cout << *it << endl;
}
return 0;
}
相关文章:
- C++循环链表删除,计数从下一个节点开始
- C++循环链接列表 - 删除所有节点
- 我写了一个函数来删除循环链接列表中的第 1 个节点,但输出显示无限次"55 44 33 22 11 99",那么我该如何解决呢?
- 循环单链表:从列表中删除特定节点
- 使用循环在链接列表的前面插入一个节点
- C++-从单链循环列表中的任何索引中删除一个节点
- 正在从循环双链接列表中删除节点
- 查询循环中的节点自引用
- 创建一个C++预排序迭代器,为循环提供树节点
- 在循环双向链表的第一个节点之前/之后插入的算法是什么?
- 删除循环双向链表中的节点
- 无法遍历树 - 节点被重新遍历了大量次,但仍然是非循环的
- 从后到前的打印节点进入无限循环
- boostpropertytree在循环中添加节点
- 我的程序在将节点读入链表后不断循环,只重复显示一个节点
- 有向循环图中两个节点之间的路径数
- C++链表节点创建导致无限循环
- 使用rapidxml在节点中循环
- 读取文件".txt"并将每行的数据分配给循环列表中的新节点C++
- 布尔函数始终为真,单向链表中的尾节点删除创建无限循环