【发布时间】:2013-08-27 01:17:41
【问题描述】:
我正在尝试使用 RapidXML 来解析我的 XML 文件。我是按照example here 做的。我没有在主函数中进行解析,而是编写了一个名为 XMLParser 的包装类来完成解析工作。这真的让我很头疼。
XMLParser.hpp:
#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include "rapidxml/rapidxml.hpp"
using namespace std;
using namespace rapidxml;
class XMLParser {
public:
XMLParser() {};
XMLParser(const std::string &xmlString): xmlCharVector(xmlString.begin(), xmlString.end())
{
//xmlCharVector.push_back('\0');
parseXML();
}
XMLParser(const std::vector<char> &_xmlVector):xmlCharVector(_xmlVector)
{
/* xmlCharVector.push_back('\0'); */ // already done in main.cpp
if (xmlCharVector != _xmlVector) //And it turns out they're the same....
std::cout << "The two vectors are not equal" << std::endl;
else
std::cout << "They are the same" << std::endl;
parseXML();
}
private:
std::vector<char> xmlCharVector;
rapidxml::xml_document<> doc;
void parseXML();
};
XMLParser.cpp:
#include "XMLParser.hpp"
using namespace std;
using namespace rapidxml;
void XMLParser::parseXML()
{
doc.parse<0>(&xmlCharVector[0]);
}
这里是 main.cpp:
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include "XMLParser.hpp"
using namespace std;
using namespace rapidxml;
int main(int argc, char **argv)
{
xml_document<> doc;
xml_node<> *root_node;
ifstream theFile("beer.xml");
vector<char> buffer((istreambuf_iterator<char>(theFile)), istreambuf_iterator<char>());
buffer.push_back('\0');
doc.parse<0>(&buffer[0]);
root_node = doc.first_node("MyBeerJournal");
xml_node<> *engine = root_node->first_node("Brewery");
//The above code works pretty well, and I can get the element I want in XML file.
//The problem occurs when I tried to use the XMLParser
XMLParser xmlParser(buffer);
return 0;
}
main 函数中的解析过程运行良好。但是当我尝试在我的包装类parseXML() 中使用该函数时,出现了错误:
在抛出 'rapidxml::parse_error' 实例后调用终止 什么():预期> 中止(核心转储)
原来我在这个函数中有其他代码,但我把它们都注释了,发现即使是单行doc.parse<0>(&xmlCharVector[0]);。为什么它在 main.cpp 中运行良好而不在包装类中运行良好?我实在想不通。有人能帮帮我吗?
【问题讨论】:
标签: c++ xml wrapper parse-error rapidxml