0

我用谷歌搜索了它,但在将xml_document<>文档保存在.xml文件中时遇到问题。此代码将国际象棋游戏 符号保存为 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 抛出了一个错误并将operator<< 标记为 error。我试图用谷歌搜索它并在 Stack Overflow 中查找有关 rapidxml 的其他问题,但我还没有找到一些令人满意的解决方案。

4

1 回答 1

4

请参阅手册:要将 DOM 树转换为“普通”XML,您需要定义在rapidxml_print.hpp

#include "rapidxml_print.hpp"

例如,您应该能够做到这一点。

rapidxml::xml_document<> doc;
...
file << doc;

(另外,避免this->. 你只需要在非常罕见的情况下)

于 2014-03-23T21:13:57.073 回答