RapidXml用法
生活随笔
收集整理的這篇文章主要介紹了
RapidXml用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、寫xml 文件
#include <iostream> #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp"using namespace rapidxml;int main() { 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;system("PAUSE");return EXIT_SUCCESS; }生成的xml例如以下:
<?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>寫文件樣例2:
#include <string> #include <iostream> #include <fstream> #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp"using namespace rapidxml; using namespace std;int main(int argc, char* argv[]) {xml_document<> doc; //是解析器char a[] = "<top>"//假設單獨傳, 就不能加上xml的頭部信息,//否則會報錯"<name>tangqiang</name>""<age>22</age>""</top>";char* p = a;doc.parse<0>(p);xml_node<>* node = doc.first_node();//去頂級結點cout << (node->name())<< endl;node = node->first_node();while (node) {cout << node->name() << node->value() << endl;//name() value()返回的字符串不會去掉首尾的空白字符node = node->next_sibling();}ofstream out("test.xml");//ofstream 默認時,假設文件存在則會覆蓋原來的內容,不存在則會新建out << doc;//doc 這樣輸出時在目標文件里不會有xml 頭信息---<?xml version='1.0' encoding='utf-8' >out.close();system("pause");return 0; }生成的xml例如以下:
<top><name>tangqiang</name><age>22</age> </top>二、讀取xml文件
#include <iostream> #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp"using namespace rapidxml;int main() {file<> fdoc("config.xml");std::cout<<fdoc.data()<<std::endl;xml_document<> doc;doc.parse<0>(fdoc.data());std::cout<<doc.name()<<std::endl;//! 獲取根節點xml_node<>* root = doc.first_node();std::cout<<root->name()<<std::endl;//! 獲取根節點第一個節點xml_node<>* node1 = root->first_node();std::cout<<node1->name()<<std::endl;xml_node<>* node11 = node1->first_node();std::cout<<node11->name()<<std::endl;std::cout<<node11->value()<<std::endl;//! 加入之后再次保存//須要說明的是rapidxml明顯有一個bug //那就是append_node(doc.allocate_node(node_element,"h","0"));的時候并不考慮該對象是否存在!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;system("PAUSE");return EXIT_SUCCESS; }生成的xml為:
<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><w>0</w><h>0</h></size><mode fullscreen="false">screen mode</mode> </config>三、刪除節點
#include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp"#include<iostream> using namespace rapidxml;int main() {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; std::ofstream out("test.xml");out<<doc;system("pause");return 0; }輸出信息例如以下:
<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><w>0</w><h>0</h></size><mode fullscreen="false">screen mode</mode> </config>刪除一個節點<config><size><x>640</x><y>480</y><w>0</w><h>0</h></size><mode fullscreen="false">screen mode</mode> </config>刪除全部節點<config/>四、編輯節點信息
臨時找到的編輯方法就是先刪除再添加
#include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp"#include<iostream> using namespace rapidxml;int main() {file<> fdoc("config.xml");std::cout<<fdoc.data()<<std::endl;xml_document<> doc;doc.parse<0>(fdoc.data());std::cout<<doc.name()<<std::endl;//! 獲取根節點xml_node<>* root = doc.first_node();xml_node<>* delnode = root->first_node("color");root->remove_node(delnode);//先刪除address節點// xml_node<>* lnode = root->first_node("size");//找到post節點xml_node<>* mynode=doc.allocate_node(node_element,"address","河北");root->insert_node(lnode,mynode);std::string text;rapidxml::print(std::back_inserter(text),doc,0);std::cout<<text<<std::endl;std::ofstream out("version.xml");out << doc;system("pause");return 0; } 輸出例如以下: <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><w>0</w><h>0</h></size><mode fullscreen="false">screen mode</mode> </config><config><address>河北</address><size><x>640</x><y>480</y><w>0</w><h>0</h></size><mode fullscreen="false">screen mode</mode> </config>五、遍歷全部節點
for(rapidxml::xml_node<char> * node = parent_node->first_node("node name");node != NULL;node = node->next_sibling()) {//TO DO }六、遍歷全部屬性
for(rapidxml::xml_attribute<char> * attr = node->first_attribute("node name");attr != NULL;attr = attr->next_attribute()) {char * value = attr->value(); }?
轉載于:https://www.cnblogs.com/rainbow70626/p/10386131.html
總結
以上是生活随笔為你收集整理的RapidXml用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RubyonRails是WebDev最佳
- 下一篇: JavaScript网页特效