visual c++ - RapidXML saving into a File -
i have googled , have problem saving xml_document<> doc in .xml file. code shall save chess games notations xml file put in compressed file adding multimedia files it. can add audio comments every game stored in 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 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); (unsigned int = 0; < 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())); (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())); (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 problem:
void filewriter::save() { file << this->doc.value(); file.close(); }
i have tried file<<this->doc;
but msvc threw error , marked operator<< error. tried google , looked other questions in stack overflow rapidxml, haven't found satisfying solution.
see manual: convert dom tree 'normal' xml, you'll need methods/operators defined in rapidxml_print.hpp
#include "rapidxml_print.hpp"
then should able this, example.
rapidxml::xml_document<> doc; ... file << doc;
(also, avoid this->
. need in pretty rare circumstances)
Comments
Post a Comment