RapidXMLを使用して XML ファイルを解析しようとしています。そして、ここの例に従ってそれを行いました。メイン関数で構文解析を行う代わりに、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;
}
メイン関数の解析プロセスは非常にうまく機能します。しかし、ラッパークラスで関数を使用しようとするとparseXML()
、エラーが発生しました:
「rapidxml::parse_error」のインスタンスをスローした後に呼び出された終了 what(): expected > Abort (コアダンプ)
もともと、この関数には他のコードがありましたが、それらすべてにコメントを付けたところ、1 行でもそれが見つかりましたdoc.parse<0>(&xmlCharVector[0]);
。ラッパークラスではなく、main.cppでうまく機能するのはなぜですか? 私は本当にそれを理解することはできません。誰でも私を助けることができますか?