Visual c++ RapidXML保存到文件中

Visual c++ RapidXML保存到文件中,visual-c++,rapidxml,Visual C++,Rapidxml,我用谷歌搜索了一下,在.xml文件中保存我的xml\u文档时遇到问题。这段代码将把国际象棋游戏符号保存为XML文件,我将把它放入一个压缩文件中,以便向其中添加多媒体文件。因此,有人可以为存储在XML中的每个游戏添加音频注释 #ifndef _FILEREADER_HPP #define _FILE_READER_HPP #include<fstream> #include"rapidxml.hpp" #include"Notation.h" namespace pgnx {

我用谷歌搜索了一下,在.xml文件中保存我的xml\u文档时遇到问题。这段代码将把国际象棋游戏符号保存为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
\ifndef\u文件阅读器\u水电站
#定义\u文件\u读取器\u HPP
#包括
#包括“rapidxml.hpp”
#包括“Notation.h”
名称空间pgnx
{
类文件编写器
{
地图数据;
流文件的std::of;
std::字符串fn;
rapidxml::xml_文档文档;
受保护的:
作废保存();
公众:
FileWriter(字符*文件名);
FileWriter();
~FileWriter(){}
无效准备();
无效插入锦标赛(锦标赛);
//操作员
文件编写器和操作员+(rhs);
FileWriter和operator=(标准::映射rhs);
FileWriter&operator=(pgnx::FileWriter-rhs);
锦标赛和运算符[](无符号整数索引);
};
}
#恩迪夫
以及功能

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::prepare()
{
使用名称空间rapidxml;
xml\u node*decl=doc.allocate\u node(node\u声明);
decl->append_属性(文档分配_属性(“版本”、“1.0”);
decl->append_属性(文档分配_属性(“编码”、“utf-8”);
单据追加节点(decl);
xml_node*pgnx=doc.allocate_节点(node_元素,“pgnx”);
pgnx->append_属性(单据分配_属性(“版本”、“版本”);
文档附加_节点(pgnx);
对于(无符号整数i=0;idata.rbegin()->first+1;i++)
{
xml_node*child=doc.allocate_node(节点元素,“锦标赛”);
child->append_属性(doc.allocate_属性(“ID”,(const char*)i));
child->append_属性(doc.allocate_属性(“Name”,me[i].getName());
for(unsigned int j=0;jappend_属性(doc.allocate_属性(“White”,(me[i])[j].getNameW());
game->append_属性(doc.allocate_属性(“Black”,(me[i])[j].getNameB());
game->append_属性(doc.allocate_属性(“Result”,(const char*)(me[i])[j].getResult());
for(unsigned int k=0;k<(me[i])[j].getLength();k++)
{
xml_node*move=doc.allocate_节点(node_元素,“move”);
move->append_属性(doc.allocate_属性(“Number”,(const char*)k));
move->append_属性(doc.allocate_属性(“White”),((me[i])[j])[k].getMoveW());
move->append_属性(doc.allocate_属性(“White”),((me[i])[j])[k].getMoveB());
游戏->附加_节点(移动);
}
子节点->附加节点(游戏);
}
pgnx->append_节点(子节点);
}
这个->文件.open(fn.c_str());
此->保存();
file.close();
doc.clear();
}
问题是:

void FileWriter::save()
{
    file << this->doc.value();
    file.close();
}
void FileWriter::save()
{
文件doc.value();
file.close();
}
我试过了
文件请参见:要将DOM树转换为“普通”XML,需要在
rapidxml\u print.hpp

#include "rapidxml_print.hpp"
那么你应该能够做到这一点,例如

rapidxml::xml_document<> doc;
...
file << doc;
rapidxml::xml\u文档文档;
...
文件
。只有在非常罕见的情况下才需要它)

请参阅:要将DOM树转换为“普通”XML,需要在
rapidxml\u print.hpp中定义的方法/运算符

#include "rapidxml_print.hpp"
那么你应该能够做到这一点,例如

rapidxml::xml_document<> doc;
...
file << doc;
rapidxml::xml\u文档文档;
...
文件
。(只有在非常罕见的情况下才需要)