我正在学习使用RapidXML,但是当我尝试读取大型XML file(我将其发布在pastebin上,因为它的长度超过400行)时,它在第4次循环时出现了第78行的细分错误,所以我觉得这有点像小缓冲区,但是我不知道该怎么办。 我的代码:
#include <rapidxml/rapidxml.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <glm/glm.hpp>
#include <algorithm>
#include <vector>
using namespace rapidxml;
int main(){
std::ifstream fule("./xml.xml");
if(!fule){
std::cout << "failed to open file";
}
std::string token, word;//to count number of layers
word = "</layer>";
int noLayers = 0;
while(fule>>token){
if(word==token){
noLayers++;
}
}
std::cout << std::endl << noLayers << std::endl;
fule.close();
std::ifstream file("./xml.xml");
if(!file){
std::cout << "failed to open file";
}
std::stringstream buffer;
buffer << file.rdbuf();
xml_document<> doc;
std::string content(buffer.str());
doc.parse<0>(&content[0]);
file.close();
xml_node<> *pRoot = doc.first_node();//<in this case <map> node, get root node
std::string gameVersion, orientation, renderOrder; //used later
int mapWidth, mapHeight, tileWidth, tileHeight, playerStartPositionX, playerStartPositionY;
xml_attribute<> *pAttr = pRoot->first_attribute();
pAttr = pRoot->first_attribute("version");
gameVersion = pAttr->value();
pAttr = pRoot->first_attribute("orientation");
orientation = pAttr->value();
pAttr = pRoot->first_attribute("renderorder");
renderOrder = pRoot->value();
pAttr = pRoot->first_attribute("width");
mapWidth = atoi(pAttr->value());
pAttr = pRoot->first_attribute("height");
mapHeight = atoi(pAttr->value());
pAttr = pRoot->first_attribute("tilewidth");
tileWidth = atoi(pAttr->value());
pAttr = pRoot->first_attribute("tileheight");
tileHeight = atoi(pAttr->value());
xml_node<> *pNode = pRoot->first_node("properties");
for(xml_node<> *propNode = pNode->first_node("property"); propNode; propNode = propNode->next_sibling()){
pAttr = propNode->first_attribute("name");
std::string s = pAttr->value();
if(s == "PlayerStartPositionX"){
pAttr = propNode->first_attribute("value");
playerStartPositionX = atoi(pAttr->value());
}if(s == "PlayerStartPositionY"){
pAttr = propNode->first_attribute("value");
playerStartPositionY = atoi(pAttr->value());
}
}
int i= 0;//to count loops of next for loop
int m_levelData[mapWidth][mapHeight][noLayers];//make it [sizeX][sizeY][numOfLayers]
for(pNode = pRoot->first_node("layer"); pNode; pNode = pNode->next_sibling()){
//TODO: get layer info <++>
xml_node<> *dataNode = pNode->first_node("data");dataNode;
pAttr = dataNode->first_attribute("encoding");
std::string s = pAttr->value();
std::vector<std::string> levelData;
std::string tmp;
if(s == "csv"){
s = dataNode->value();
std::replace(s.begin(), s.end(), ',', ' ');
std::stringstream ss(s);
while(std::getline(ss, tmp)){
if(!tmp.empty()){
levelData.push_back(tmp);
}
}
for(int j = 0; j < /*sizeY*/ 100; j++){
std::stringstream in(levelData[j]);
std::vector<int> v;
int temp;
while(in >> temp){
v.push_back(temp);
}
for(int k = 0; k < /*sizex*/ 100;k++){
m_levelData[k][j][i] = v[k];
}
}
} else {
std::cout << "*AUTISTIC SCREAMING*" << std::endl;
}
i++;
}
}
(这只是弄清楚RapidXML如何工作的临时代码)
XML文件是由Tiled map editor
生成的答案 0 :(得分:0)
您的XML似乎格式错误。当我在开始数据标签之后和结束标签之前删除新行时,程序按预期执行。没有抛出任何错误。