rapidxml学习记录
生活随笔
收集整理的這篇文章主要介紹了
rapidxml学习记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、資料
官網: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>總結
以上是生活随笔為你收集整理的rapidxml学习记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦醒暗黑廿年(补)
- 下一篇: WebLogic部署配置