pugixml库

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

以前觉得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 plain  copy
  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 plain  copy
  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/


           继续学习pugixml库使用。xml文件的节点一般有两种方式:

            <paramname="version" type="float" value="1.1" />和

                        <Welcome>Welcometo MyApp</Welcome>

 

            今天我们学习如何使用pugixml库给一个xml文件添加这两种节点。

        xml的原文件是:

[html]  view plain  copy
  1. <?xml version="1.0"?>  
  2. <MyApp>  
  3.     <Messages>  
  4.         <Welcome>Welcome to MyApp</Welcome>  
  5.         <Farewell>Thank you for using MyApp</Farewell>  
  6.     </Messages>  
  7.     <Windows>  
  8.         <Window name="MainFrame" x="5" y="15" w="400" h="250" />  
  9.     </Windows>  
  10. </MyApp>  

           我们要达到的效果是:

[html]  view plain  copy
  1. <?xml version="1.0"?>  
  2. <MyApp>  
  3.     <Messages>  
  4.         <Welcome>Welcome to MyApp</Welcome>  
  5.         <Farewell>Thank you for using MyApp</Farewell>  
  6.     </Messages>  
  7.     <Windows>  
  8.         <Window name="MainFrame" x="5" y="15" w="400" h="250" />  
  9.         <param name="version" type="float" value="1.1" />  
  10.         <Welcome>Welcome to MyApp</Welcome>  
  11.         <param name="version" type="float" value="1.1" />  
  12.         <Farewell>Thank you for using MyApp</Farewell>  
  13.     </Windows>  
  14. </MyApp>  

              实现代码如下,代码较为简单,这里就不作详细解释了:

[cpp]  view plain  copy
  1. #include <vector>  
  2. #include <string>  
  3. #include <pugixml.hpp>  
  4.   
  5.   
  6. int _tmain(int argc, _TCHAR* argv[])  
  7. {  
  8.     pugi::xml_document doc;  
  9.     // 加载xml文件  
  10.     pugi::xml_parse_result ret = doc.load_file(_T("E:\\ConfigDemo.xml"));  
  11.   
  12.     pugi::xml_node Messages = doc.child(_T("MyApp")).child(_T("Messages"));  
  13.     std::vector<std::pair<std::string,std::string> > vecNode;  
  14.   
  15.     // 读取原有的Welcome 节点和Farewell节点,并保存其值  
  16.     for (pugi::xml_node Plugin = Messages.first_child(); Plugin; Plugin = Plugin.next_sibling())  
  17.     {  
  18.         typedef std::pair<std::string,std::string> string_Pair;  
  19.         std::string strNodeName = Plugin.name();  
  20.         std::string strValue = Messages.child_value(strNodeName.c_str());  
  21.         vecNode.push_back(string_Pair(strNodeName,strValue));  
  22.     }  
  23.   
  24.     pugi::xml_node window = doc.child(_T("MyApp")).child(_T("Windows"));  
  25.     for (int i =0;i<vecNode.size();i++)  
  26.     {  
  27.         // 添加Welcome 节点和Farewell节点  
  28.         pugi::xml_node new_node = window.append_child(vecNode[i].first.c_str());  
  29.   
  30.         new_node.append_child(pugi::node_pcdata).set_value(vecNode[i].second.c_str());  
  31.         // 添加param 节点  
  32.         pugi::xml_node param = window.insert_child_before("param",new_node);  
  33.         param.append_attribute("name") = "version";  
  34.         param.append_attribute("value") = 1.1;  
  35.         param.insert_attribute_after("type", param.attribute("name")) = "float";  
  36.     }  
  37.   
  38.     doc.save_file(_T("E:\\ConfigDemo.xml"));  
  39.     getchar();  
  40.     return 0;  
  41. }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值