我用谷歌搜索了,我在 .xml 文件中保存 xml_document<> 文档时遇到问题。本规范应将国际象棋游戏符号保存为XML文件,我将其放入压缩文件中,以便将多媒体文件添加到其中。因此,有人可以为存储在XML中的每个游戏添加音频评论。
#ifndef _FILEREADER_HPP
#define _FILE_READER_HPP
#include<fstream>
#include"rapidxml.hpp"
#include"Notation.h"
namespace pgnx
{
class FileWriter
{
std::map<unsigned int, Tournament> data;
std::ofstream file;
std::string fn;
rapidxml::xml_document<> doc;
protected:
void save();
public:
FileWriter(char* filename);
FileWriter();
~FileWriter(){}
void prepare();
void insertTournament(Tournament);
//Operators
FileWriter& operator+(Tournament rhs);
FileWriter& operator=(std::map<unsigned int, Tournament> rhs);
FileWriter& operator=(pgnx::FileWriter rhs);
Tournament& operator[](unsigned int index);
};
}
#endif
和功能
void FileWriter::prepare()
{
using namespace rapidxml;
xml_node<>* decl = doc.allocate_node(node_declaration);
decl->append_attribute(doc.allocate_attribute("version", "1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(decl);
xml_node<>* pgnx = doc.allocate_node(node_element, "pgnx");
pgnx->append_attribute(doc.allocate_attribute("version", _VERSION_));
doc.append_node(pgnx);
for (unsigned int i = 0; i < this->data.rbegin()->first + 1; i++)
{
xml_node<>* child = doc.allocate_node(node_element, "Tournament");
child->append_attribute(doc.allocate_attribute("ID", (const char*)i));
child->append_attribute(doc.allocate_attribute("Name", me[i].getName()));
for (unsigned int j = 0; j < me[i].getLength(); j++)
{
xml_node<>* game = doc.allocate_node(node_element, "Game");
game->append_attribute(doc.allocate_attribute("White", (me[i])[j].getNameW()));
game->append_attribute(doc.allocate_attribute("Black", (me[i])[j].getNameB()));
game->append_attribute(doc.allocate_attribute("Result", (const char*)(me[i])[j].getResult()));
for (unsigned int k = 0; k < (me[i])[j].getLength(); k++)
{
xml_node<>* move = doc.allocate_node(node_element, "move");
move->append_attribute(doc.allocate_attribute("Number", (const char*)k));
move->append_attribute(doc.allocate_attribute("White", ((me[i])[j])[k].getMoveW()));
move->append_attribute(doc.allocate_attribute("White", ((me[i])[j])[k].getMoveB()));
game->append_node(move);
}
child->append_node(game);
}
pgnx->append_node(child);
}
this->file.open(fn.c_str());
this->save();
file.close();
doc.clear();
}
这就是问题所在:
void FileWriter::save()
{
file << this->doc.value();
file.close();
}
我试过了
file<<this->doc;
但是MSVC引发了错误并且标记了运营商&lt;&lt;作为错误。
我尝试谷歌它并在Stack Overflow中查找关于rapidxml的其他问题,但我还没有找到一些令人满意的解决方案。
答案 0 :(得分:3)
请参阅manual:要将DOM树转换为“普通”XML,您需要rapidxml_print.hpp
中定义的方法/运算符
#include "rapidxml_print.hpp"
然后你应该能够做到这一点,例如。
rapidxml::xml_document<> doc;
...
file << doc;
(另外,避免this->
。在非常罕见的情况下你只需要这样做)