Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用rapidxml在节点中循环 我是新的使用XML的C++,我想循环通过一个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>_C++_Xml_Rapidxml - Fatal编程技术网

使用rapidxml在节点中循环 我是新的使用XML的C++,我想循环通过一个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>

使用rapidxml在节点中循环 我是新的使用XML的C++,我想循环通过一个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>,c++,xml,rapidxml,C++,Xml,Rapidxml,这就是我加载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('\0'); xmlDoc.par

这就是我加载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('\0');
xmlDoc.parse<0>(&buffer[0]);
rapidxml::xml\u文档xmlDoc;
/*“将文件读入向量”*/
std::vector buffer((std::istreambuf_迭代器(xmlFile)),std::istreambuf_迭代器();
buffer.push_back('\0');
xmlDoc.parse(&buffer[0]);

如何循环节点?

将xml加载到文档对象后,可以使用
第一个节点()
获取指定的子节点(或仅第一个);然后您可以使用
next\u sibling()
遍历它的所有同级。使用
first\u 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('\0');
    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;
} 
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::endl;
使用std::ifstream;
使用std::vector;
使用std::stringstream;
使用名称空间rapidxml;
int main()
{
ifstream-in(“test.xml”);
xml_文档文档;
std::vector buffer((std::istreambuf_迭代器(in)),std::istreambuf_迭代器());
buffer.push_back('\0');
文档解析(&buffer[0]);
向量向量ID;
//获取文档的第一个节点-“播放器”节点
//获取玩家的第一个子节点-“帧”节点
//获取帧的第一个子节点-第一个帧节点
xml_node*nodeFrame=doc.first_node()->first_node()->first_node();
while(nodeFrame)
{
细流ss;
ss first_属性(“id”)->value();
int nID;
ss>>nID;
向量推回(nID);
nodeFrame=nodeFrame->next_sibling();
}
vector::const_迭代器it=vecID.begin();
for(;it!=vecID.end();it++)
{

也许是时候接受博扬的回答了?