I have googled it and I have a problem saving my xml_document<> doc in a .xml file. This Code shall save Chess Games Notations as XML file which I will put in a compressed file for adding multimedia files into it. So someone can add Audio Comments for every game stored in the 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
and the Functions
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();
}
and here is the problem:
void FileWriter::save()
{
file << this->doc.value();
file.close();
}
I have tried
file<<this->doc;
but MSVC threw an error and marked operator<< as error.
I tried to google it and looked up other questions in Stack Overflow about rapidxml, but I haven't found some satisfying solution.