pugixml简介

来源:互联网 发布:中国域名的属性 编辑:程序博客网 时间:2024/05/09 05:46

来源:http://blog.csdn.net/clever101

很久没写博客了,难得今天有空,心情也不错。写什么内容呢?就写写最近接触的一个很棒的xml操作库——pugixml。

 

         以前觉得tinyxml也是一个挺好的操作xml文件的库。最近找到了pugixml库,发现pugixml库对tinyxml可谓是全面胜出。

 

一.支持字符集:tinyxml不支持unicode(这个可谓是很多人不愿意用tinyxml的原因之一),pugixml支持UTF8 encoding、Little-endian UTF16、Big-endian UTF16、UTF16 with native endianness、Little-endianUTF32、Big-endian UTF32和UTF32with native endianness。

  

二.操作xml文件的性能。

 

              Xml库解析性能比较表  

         

(表格来自:http://rapidxml.sourceforge.net/manual.html)

 

       看看上表吧,pugixml比tinyxml快不止一个数量级,仅比最快的RapidXml慢一点。pugixml比RapidXml的一个优点是pugixml支持xpath, RapidXml不支持xpath。

 

一.使用的方便性。虽然pugixml和tinyxml都是基于面向对象的,但pugixml的使用方便性远胜tinyxml。比如在查找节点的属性值方面,Tinyxml需要调用者从根节点开始查找(使用TiXmlElement类),然后递归找下去,找到到取出属性值。而pugixml使用一个child函数把查找节点这一步都封装好了。比如下面这样一个xml文件:

      
[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8" standalone="no" ?>  
  2. <Profile FormatVersion="1">  
  3.     <Tools>  
  4.         <Tool Filename="jam" AllowIntercept="true">  
  5.             <Description>Jamplus build system</Description>  
  6.         </Tool>  
  7.         <Tool Filename="mayabatch.exe" AllowRemote="true" OutputFileMasks="*.dae" DeriveCaptionFrom="lastparam" Timeout="40" />  
  8.         <Tool Filename="meshbuilder_*.exe" AllowRemote="false" OutputFileMasks="*.mesh" DeriveCaptionFrom="lastparam" Timeout="10" />  
  9.         <Tool Filename="texbuilder_*.exe" AllowRemote="true" OutputFileMasks="*.tex" DeriveCaptionFrom="lastparam" />  
  10.         <Tool Filename="shaderbuilder_*.exe" AllowRemote="true" DeriveCaptionFrom="lastparam" />  
  11.     </Tools>  
  12. </Profile>  

     使用pugixml简单几句代码就能将所有Tool节点的属性值都输出来:

[cpp] view plaincopy
  1. pugi::xml_document doc;  
  2.     if (!doc.load_file("xgconsole.xml")) return -1;  
  3.   
  4.     pugi::xml_node tools = doc.child("Profile").child("Tools");  
  5.   
  6.     //[code_traverse_base_basic  
  7.     for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())  
  8.     {  
  9.         std::cout << "Tool:";  
  10.   
  11.         for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())  
  12.         {  
  13.             std::cout << " " << attr.name() << "=" << attr.value();  
  14.         }  
  15.   
  16.         std::cout << std::endl;  
  17.     }  
  18. //]  
          是不是很方便呢?

 

pugixml的官方主页为:http://pugixml.org/


原创粉丝点击