Sep*_*est 1 rapidxml visual-c++
我用谷歌搜索了一下,在将xml_document<>文档保存在.xml文件中时遇到问题。这段代码将把Chess Games Notations 保存为 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
Run Code Online (Sandbox Code Playgroud)
和功能
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();
}
Run Code Online (Sandbox Code Playgroud)
问题是这样的:
void FileWriter::save()
{
file << this->doc.value();
file.close();
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过
file<<this->doc;
,但 MSVC 抛出了一个错误并将operator<< 标记为 error。我尝试用谷歌搜索并在 Stack Overflow 中查找有关rapidxml 的其他问题,但我还没有找到一些令人满意的解决方案。
请参阅手册:要将 DOM 树转换为“正常”XML,您需要在中定义的方法/运算符rapidxml_print.hpp
#include "rapidxml_print.hpp"
Run Code Online (Sandbox Code Playgroud)
例如,那么您应该能够做到这一点。
rapidxml::xml_document<> doc;
...
file << doc;
Run Code Online (Sandbox Code Playgroud)
(另外,避免this->
。只有在极少数情况下才需要)
归档时间: |
|
查看次数: |
5550 次 |
最近记录: |