使用 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 plain copy
  1.     //! Enumeration listing all node types produced by the parser.  
  2.   
  3.     //! Use xml_node::type() function to query node type.  
  4.   
  5.     enum node_type  
  6.   
  7.     {  
  8.   
  9.         node_document,     //!< A document node. Name and value are empty.  
  10.   
  11.         node_element,      //!< An element node. Name contains element name. Value contains text of first data node.  
  12.   
  13.         node_data,        //!< A data node. Name is empty. Value contains data text.  
  14.   
  15.         node_cdata,       //!< A CDATA node. Name is empty. Value contains data text.  
  16.   
  17.         node_comment,    //!< A comment node. Name is empty. Value contains comment text.  
  18.   
  19.         node_declaration,  //!< A declaration node. Name and value are empty. Declaration parameters (version, encoding and standalone) are in node attributes.  
  20.   
  21.         node_doctype,     //!< A DOCTYPE node. Name is empty. Value contains DOCTYPE text.  
  22.   
  23.         node_pi          //!< A PI node. Name contains target. Value contains instructions.  
  24.   
  25. };  
  

简单操作:

 

[cpp]  view plain copy
  1. #include <iostream>  
  2.   
  3. #include <string>  
  4.   
  5. #include <rapidxml/rapidxml.hpp>  
  6.   
  7. #include <rapidxml/rapidxml_utils.hpp>  
  8.   
  9. #include <rapidxml/rapidxml_print.hpp>  
  10.   
  11.    
  12.   
  13. using namespace std;  
  14.   
  15.    
  16.   
  17. int main()  
  18.   
  19. {  
  20.   
  21.     using namespace rapidxml;  
  22.   
  23.     xml_document<> doc;  
  24.   
  25.     xml_node<> *node = doc.allocate_node(node_element,"a","Google公司");  
  26.   
  27.     doc.append_node(node);  
  28.   
  29.    
  30.   
  31.     xml_attribute<> *attr = doc.allocate_attribute("href","google.com");  
  32.   
  33.     node->append_attribute(attr);  
  34.   
  35.    
  36.   
  37.     //直接输出  
  38.   
  39.     cout<<"print:doc"<<doc<<endl;  
  40.   
  41.    
  42.   
  43.     //用保存到string  
  44.   
  45.     string strxml;  
  46.   
  47.     print(std::back_inserter(strxml),doc,0);  
  48.   
  49.     cout<<"print:strxml"<<strxml<<endl;  
  50.   
  51.    
  52.   
  53.     //支持c风格的这里不演示,自己看文档吧  
  54.   
  55.     return 0;  
  56.   
  57. }  

运行结果:

 

 

 

读取文件(这里是从其他拷过来我刚学的时候就保存下来了,忘了在那里考的网上有好几个位兄弟都有的感觉,我也借来用用):

 

基本的步骤为
首先获取xml文件数据
然后分析数据
获取节点
获取属性
获取名字
获取值
...

代码如下:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <rapidxml/rapidxml.hpp>  
  3. #include <rapidxml/rapidxml_utils.hpp>  
  4. #include <rapidxml/rapidxml_print.hpp>  
  5.    
  6. using namespace rapidxml;  
  7.   
  8. int main()  
  9. {      
  10.      file<> fdoc("config.xml");  
  11.      std::cout<<fdoc.data()<<std::endl;   
  12.      xml_document<>  doc;      
  13.      doc.parse<0>(fdoc.data());   
  14.   
  15.      std::cout<<doc.name()<<std::endl;  
  16.        
  17.      //! 获取根节点  
  18.      xml_node<>* root = doc.first_node();  
  19.      std::cout<<root->name()<<std::endl;  
  20.   
  21.      //! 获取根节点第一个节点  
  22.      xml_node<>* node1 = root->first_node();  
  23.      std::cout<<node1->name()<<std::endl;   
  24.   
  25.      xml_node<>* node11 = node1->first_node();  
  26.      std::cout<<node11->name()<<std::endl;  
  27.      std::cout<<node11->value()<<std::endl;  
  28.        
  29.      //! 修改之后再次保存  
  30.      xml_node<>* size = root->first_node("size");  
  31.      size->append_node(doc.allocate_node(node_element,"w","0"));  
  32.      size->append_node(doc.allocate_node(node_element,"h","0"));  
  33.   
  34.      std::string text;    
  35.      rapidxml::print(std::back_inserter(text),doc,0);    
  36.    
  37.      std::cout<<text<<std::endl;   
  38.       
  39.      std::ofstream out("config.xml");  
  40.      out << doc;  
  41.   
  42.      system("PAUSE");  
  43.      return EXIT_SUCCESS;  
  44. }  

 

[xhtml]  view plain copy
  1. 生成的xml为:  
  2.   
  3. <?xml version='1.0' encoding='utf-8' ?>  
  4. <config>  
  5.     <color>  
  6.         <red>0.1</red>  
  7.         <green>0.1</green>  
  8.         <blue>0.1</blue>  
  9.         <alpha>1.0</alpha>  
  10.     </color>  
  11.     <size>  
  12.         <x>640</x>  
  13.         <y>480</y>  
  14.     </size>  
  15.     <mode fullscreen="false">screen mode</mode>  
  16. </config>  

 

需要说明的是rapidxml明显有一个bug
那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值