日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用 rapidxml 做配置文件

發布時間:2023/12/10 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 rapidxml 做配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對于配置文件,一般會選用ini,xml?等等的配置格式。如何快速高效的從文件內讀取自己想要的信息是每個做配置文件想要達到的效果。對以小型開發我們并不用時用到msxml這種重量級的解析器。那樣會給自己添麻煩的。這里我推薦大家使用rapidxml。

之前使用tinyxml?感覺還可以。后看了rapidxml?就想換用這個開源庫。經常編寫跨平臺軟件省去編譯和加載lib。4個文件實現解析xml而且超高效官網還拿他跟strlen比較速度牛X吧看看下面的表。

?

?

其他廢話我就不多講。其實之前也有人介紹這個庫怎么使用。這里我就給幾個鏈接。

燕良博客:http://blog.csdn.net/Neil3D/archive/2010/03/11/5369173.aspx

官網:http://rapidxml.sourceforge.net/

?

如果不懂xml的概念的朋友自己到網上找下相關的教程學習下。這里我簡單的介紹下各個文件的作用:自己留個底怕以后自己會忘。

?

庫可以配置宏:

RAPIDXML_NO_EXCEPTIONS //不使用異常

RAPIDXML_STATIC_POOL_SIZE //內存池大小默認(64 * 1024)

一般都不用改

?

rapidxml.hpp:

?????????只要文件?實現內存池?解析string?異常處理

xml_base?基數節點類

xml_attribute?文本類

xml_node?節點類

xml_document?文檔類

rapidxml_iterators.hpp:

?????????提供兩個迭代器類:node_iterator,?attribute_iterator

rapidxml_print.hpp:

?????????提供跟字符串,流對象的裝換函數

rapidxml_utils.hpp:

?????????提供一個file用來讀取文件使用

?????????跟兩個計數函數?count_children,count_attributes

?

節點類型源碼講的很清楚

[cpp]?view plaincopy
  • ????//!?Enumeration?listing?all?node?types?produced?by?the?parser.??
  • ??
  • ????//!?Use?xml_node::type()?function?to?query?node?type.??
  • ??
  • ????enum?node_type??
  • ??
  • ????{??
  • ??
  • ????????node_document,?????//!<?A?document?node.?Name?and?value?are?empty.??
  • ??
  • ????????node_element,??????//!<?An?element?node.?Name?contains?element?name.?Value?contains?text?of?first?data?node.??
  • ??
  • ????????node_data,????????//!<?A?data?node.?Name?is?empty.?Value?contains?data?text.??
  • ??
  • ????????node_cdata,???????//!<?A?CDATA?node.?Name?is?empty.?Value?contains?data?text.??
  • ??
  • ????????node_comment,????//!<?A?comment?node.?Name?is?empty.?Value?contains?comment?text.??
  • ??
  • ????????node_declaration,??//!<?A?declaration?node.?Name?and?value?are?empty.?Declaration?parameters?(version,?encoding?and?standalone)?are?in?node?attributes.??
  • ??
  • ????????node_doctype,?????//!<?A?DOCTYPE?node.?Name?is?empty.?Value?contains?DOCTYPE?text.??
  • ??
  • ????????node_pi??????????//!<?A?PI?node.?Name?contains?target.?Value?contains?instructions.??
  • ??
  • };??
  • ??

    簡單操作:

    ?

    [cpp]?view plaincopy
  • #include?<iostream>??
  • ??
  • #include?<string>??
  • ??
  • #include?<rapidxml/rapidxml.hpp>??
  • ??
  • #include?<rapidxml/rapidxml_utils.hpp>??
  • ??
  • #include?<rapidxml/rapidxml_print.hpp>??
  • ??
  • ???
  • ??
  • using?namespace?std;??
  • ??
  • ???
  • ??
  • int?main()??
  • ??
  • {??
  • ??
  • ????using?namespace?rapidxml;??
  • ??
  • ????xml_document<>?doc;??
  • ??
  • ????xml_node<>?*node?=?doc.allocate_node(node_element,"a","Google公司");??
  • ??
  • ????doc.append_node(node);??
  • ??
  • ???
  • ??
  • ????xml_attribute<>?*attr?=?doc.allocate_attribute("href","google.com");??
  • ??
  • ????node->append_attribute(attr);??
  • ??
  • ???
  • ??
  • ????//直接輸出??
  • ??
  • ????cout<<"print:doc"<<doc<<endl;??
  • ??
  • ???
  • ??
  • ????//用保存到string??
  • ??
  • ????string?strxml;??
  • ??
  • ????print(std::back_inserter(strxml),doc,0);??
  • ??
  • ????cout<<"print:strxml"<<strxml<<endl;??
  • ??
  • ???
  • ??
  • ????//支持c風格的這里不演示,自己看文檔吧??
  • ??
  • ????return?0;??
  • ??
  • }??
  • 運行結果:

    ?

    ?

    ?

    讀取文件(這里是從其他拷過來我剛學的時候就保存下來了,忘了在那里考的網上有好幾個位兄弟都有的感覺,我也借來用用):

    ?

    基本的步驟為
    首先獲取xml文件數據
    然后分析數據
    獲取節點
    獲取屬性
    獲取名字
    獲取值
    ...

    代碼如下:

    [cpp]?view plaincopy
  • #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;??
  • ???????
  • ?????//!?修改之后再次保存??
  • ?????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;??
  • }??
  • ?

    [xhtml]?view plaincopy
  • 生成的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>??
  • ?

    需要說明的是rapidxml明顯有一個bug
    那就是append_node(doc.allocate_node(node_element,"h","0"));的時候并不考慮該對象是否存在!

    總結

    以上是生活随笔為你收集整理的使用 rapidxml 做配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。