官网:http://rapidxml.sourceforge.net/
参考:
https://blog.csdn.net/wqvbjhc/article/details/7662931
http://blog.sina.com.cn/s/blog_9b0604b40101o6fm.html
rapidxml_print.hpp在// Internal printing operations
后添加声明
template<class OutIt, class Ch>
OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
template<class OutIt, class Ch>
OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);
main.cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <memory>
#include <sstream>
using namespace std;
//下面三个文件是本段代码需要的库文件
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"
using namespace rapidxml;
int CreateXml();
int ReadAndChangeXml();
void DeleteNode();
void EditNode();
void PrintAllNode();
int main()
{
CreateXml();
//ReadAndChangeXml();
//DeleteNode();
//EditNode();
PrintAllNode();
cout << "hello" << endl;
return 0;
}
//创建一个名称为config.xml文件
int CreateXml()
{
xml_document<> doc;
xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'"));
doc.append_node(rot);
xml_node<>* node = doc.allocate_node(node_element,"config","information");
xml_node<>* color = doc.allocate_node(node_element,"color",NULL);
doc.append_node(node);
node->append_node(color);
color->append_node(doc.allocate_node(node_element,"red","0.1"));
color->append_node(doc.allocate_node(node_element,"green","0.1"));
color->append_node(doc.allocate_node(node_element,"blue","0.1"));
color->append_node(doc.allocate_node(node_element,"alpha","1.0"));
xml_node<>* size = doc.allocate_node(node_element,"size",NULL);
size->append_node(doc.allocate_node(node_element,"x","640"));
size->append_node(doc.allocate_node(node_element,"y","480"));
node->append_node(size);
xml_node<>* mode = doc.allocate_node(rapidxml::node_element,"mode","screen mode");
mode->append_attribute(doc.allocate_attribute("fullscreen","false"));//添加属性
node->append_node(mode);
std::string text;
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl;
std::ofstream out("config.xml");
out << doc;
}
//读取并修改config.xml
int ReadAndChangeXml()
{
file<> fdoc("config.xml");
std::cout<<fdoc.data()<<std::endl;
xml_document<> doc;
doc.parse<0>(fdoc.data());
std::cout<<doc.name()<<std::endl;
//! 获取根节点
rapidxml::xml_node<>* root = doc.first_node();
std::cout<<root->name()<<std::endl;
//! 获取根节点第一个节点(没有利用first查找功能)
rapidxml::xml_node<>* node1 = root->first_node();
std::cout<<node1->name()<<std::endl;
rapidxml::xml_node<>* node11 = node1->first_node();
std::cout<<node11->name()<<std::endl;
std::cout<<node11->value()<<std::endl;
//! 在size结点下添加(利用first查找功能)
xml_node<>* size = root->first_node("size");
size->append_node(doc.allocate_node(node_element,"w","0"));
size->append_node(doc.allocate_node(node_element,"h","0"));
std::string text;
rapidxml::print(std::back_inserter(text),doc,0);
std::cout<<text<<std::endl;
std::ofstream out("config.xml");
out << doc;
}
void DeleteNode()
{
file<> fdoc("config.xml");
xml_document<> doc;
doc.parse<0>(fdoc.data());
std::string text;
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl;
xml_node<>* root = doc.first_node();
xml_node<>* sec = root->first_node();
root->remove_node(sec); //移除根节点下的sec结点(包括该结点下所有结点)
text="删除一个节点\r\n";
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl;
root->remove_all_nodes(); //移除根节点下所有结点
text="删除所有节点\r\n";
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl;
}
void EditNode()
{
file<> fdoc("config.xml");
xml_document<> doc;
doc.parse<0>(fdoc.data());
xml_node<>* root = doc.first_node();
xml_node<>* delnode = root->first_node("color");
root->remove_node(delnode);//先删除color节点
xml_node<>* lnode = root->first_node("size");//找到size节点
xml_node<>* mynode=doc.allocate_node(node_element,"address","河北");
root->insert_node(lnode,mynode);//添加address兄弟节点
std::string text;
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl;
}
void PrintAllNode()
{
file<> fdoc("config.xml");
xml_document<> doc;
doc.parse<0>(fdoc.data());
xml_node<>* root = doc.first_node();
//依次找到兄弟结点,若兄弟结点仍有子节点,则无法打印值,需递归
for(rapidxml::xml_node<char> * node = root->first_node("color");node != NULL;node = node->next_sibling())
{
std::cout<<node->name() << ": " << node->value()<<std::endl;
}
}
原型输出:
<?xml version='1.0' encoding='utf-8' ?>
<config>
<color>
<red>0.1</red>
<green>0.1</green>
<blue>0.1</blue>
<alpha>1.0</alpha>
</color>
<size>
<x>640</x>
<y>480</y>
</size>
<mode fullscreen="false">screen mode</mode>
</config>