如何将RapidXML中的内容保存到xml文件?我的应用程序是MFC,我使用CString很多,因为它更容易操纵字符串。因此,如果我有下面的代码,我该如何保存到xml文件?
#include "rapidxml.hpp"
rapidxml::xml_document<> doc;
rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_type::node_declaration, 0,0,0,0);
decl->append_attribute(doc.allocate_attribute("xml version", "1.0"));
doc.append_node(decl);
rapidxml::xml_node<>* comment = doc.allocate_node(rapidxml::node_type::node_comment, 0, "Application Config");
doc.append_node(comment);
rapidxml::xml_node<>* subsytem = doc.allocate_node(rapidxml::node_type::node_element, "Subsystem");
subsytem->append_attribute(doc.allocate_attribute("Name", "My App"));
subsytem->append_attribute(doc.allocate_attribute("Version", "1.0"));
doc.append_node(subsytem);
感谢。
答案 0 :(得分:3)
using namespace rapidxml;
int testmain()
{
xml_document<> doc;
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<> *files = doc.allocate_node(node_element, "Files");
doc.append_node(files);
xml_attribute<> *attr = doc.allocate_attribute("dummy", "google.com");
files->append_attribute(attr);
for(int i = 0;i<10;++i)
{
xml_node<> *file = doc.allocate_node(node_element, "File");
files->append_node(file);
xml_node<> *path = doc.allocate_node(node_element, "Path","File_path");
file->append_node(path);
xml_node<> *name = doc.allocate_node(node_element, "Name""File_name");
file->append_node(name);
}
std::ofstream myfile;
myfile.open ("c:/example.xml");
myfile << doc;
//print(std::cout, doc, 0);
return 0;
};