boost::property_tree实例用法

来源:互联网 发布:淘宝定制类买家要退款 编辑:程序博客网 时间:2024/06/05 11:51

boost::property_tree 的解析xml模块是基于rapidXml的, 以下是两个例子:

例子1: 

[cpp] view plaincopy
  1. #include <boost/property_tree/ptree.hpp>  
  2. #include <boost/property_tree/xml_parser.hpp>  
  3. #include <boost/typeof/typeof.hpp>  
  4. #include <iostream>  
  5.   
  6. using namespace std;  
  7.   
  8. void ReadConfig()  
  9. {  
  10.   boost::property_tree::ptree pt;  
  11.         boost::property_tree::read_xml("del.conf", pt);  
  12.         filenum = pt.get<int>("root.delfile.filenum");  
  13.   
  14.         cout << "filenum: " << filenum << endl;  
  15.           
  16.         BOOST_AUTO(child, pt.get_child("root.delfile.paths"));  
  17.         for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)  
  18.         {  
  19.          BOOST_AUTO(child_paths, pos->second.get_child("")); //此处不需要填结点名,但引号不能省.  
  20.          for (BOOST_AUTO(pos_paths, child_paths.begin()); pos_paths != child_paths.end(); ++pos_paths)  
  21.           cout << pos_paths->second.data() << endl;  
  22.         }  
  23.           
  24. }  
  25. int main()  
  26. {  
  27.  ReadConfig();  
  28.  return 0;  
  29. }  
  30.   
  31.   
  32. /* 
  33. 附录:配置文件del.conf 
  34. */  

[html] view plaincopy
  1. <root>  
  2.  <delfile>  
  3.   
  4.   <filenum> 35 </filenum>  
  5.   
  6.   <paths>  
  7.    <path>   
  8.     <pathname>/tmp/tmp0/</pathname>  
  9.     <before_hours> 0 </before_hours>  
  10.    </path>  
  11.     
  12.    <path>   
  13.     <pathname>/tmp/tmp1/</pathname>  
  14.     <before_hours> 1 </before_hours>  
  15.    </path>  
  16.     
  17.    <path>   
  18.     <pathname>/tmp/tmp2/</pathname>  
  19.     <before_hours> 2 </before_hours>  
  20.    </path>  
  21.     
  22.    <path>   
  23.     <pathname>/tmp/tmp3/</pathname>  
  24.     <before_hours> 3 </before_hours>  
  25.    </path>  
  26.     
  27.    <path>   
  28.     <pathname>/tmp/tmp4/</pathname>  
  29.     <before_hours> 4 </before_hours>  
  30.    </path>  
  31.   </paths>  
  32.   
  33.  </delfile>  
  34.   
  35.   
  36.  <backup>  
  37.   <backuptime> 23:59 </backuptime>  
  38.  </backup>  
  39.   
  40. </root>  


例子2: 

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <boost/property_tree/ptree.hpp>  
  4. #include <boost/property_tree/xml_parser.hpp>  
  5. #include <boost/foreach.hpp>  
  6.   
  7. using namespace std;  
  8. using namespace boost::property_tree;  
  9.   
  10. int main(void){  
  11.     ptree pt;  
  12.     read_xml("debug_settings.xml", pt);  
  13.   
  14.         //loop for every node under debug  
  15.     BOOST_FOREACH(ptree::value_type &v1, pt.get_child("debug")){  
  16.   
  17.         if(v1.first == "<xmlattr>"){ //it's an attribute  
  18.             //read debug name="debugname"  
  19.             cout<< "debug name=" << v1.second.get<string>("name") << endl;  
  20.         }else if(v1.first == "file"){  
  21.             //read file name="debug.log"  
  22.             cout << "  file name=" << v1.second.get<string>("<xmlattr>.name") << endl;  
  23.         }  
  24.         else// v1.first == "modules"  
  25.             //get module type  
  26.             cout<< "  module type:" << v1.second.get<string>("<xmlattr>.type") << endl;  
  27.   
  28.             //loop for every node under modules  
  29.             BOOST_FOREACH(ptree::value_type &v2, v1.second){  
  30.                 if(v2.first == "<xmlattr>"){  //it's an attribute  
  31.                     //this can also get module type  
  32.                     cout<< "  module type again:" << v2.second.get<string>("type") << endl;  
  33.                 }  
  34.                 else{  
  35.                     //all the modules have the same structure, so just use data() function.  
  36.                     cout<< "    module name:" << v2.second.data() << endl;  
  37.                 }  
  38.             }//end BOOST_FOREACH  
  39.         }  
  40.     }//end BOOST_FOREACH  
  41. }  



[html] view plaincopy
  1. <debug name="debugname">    
  2.     <file name="debug.log"/>    
  3.     <modules type="internal">    
  4.         <module1>Finance_Internal</module1>    
  5.         <module2>Admin_Internal</module2>    
  6.         <module3>HR_Internal</module3>    
  7.     </modules>    
  8.         
  9.     <modules type="external">    
  10.         <module>Finance_External</module>    
  11.         <module>Admin_External</module>    
  12.         <module>HR_External</module>      
  13.     </modules>    
  14. </debug>    



----------------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------------

关于boost::property_tree, 转自:  http://notes.xj-labs.net/?p=52


Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的。目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0。


  • 主要作用/应用场合

Boost.PropertyTree 提供了一种结构化的数据存储容器。并且提供了一系列的解释器可以将内存中的结构与具体格式相互转换 (比如: INI, XML, JSON )。

至少可以用在:

  1. 进程间通讯或者跨语言的进程间的通讯
  2. 一些配置文件的存取
  3. 网络通讯协议的格式

  • 基本用法

基本用法有 2 种场景。第一种是从 Property Tree存储到具体格式。第二种是从具体格式解析到具体的 Property Tree。其他还有一些 Property Tree 操作的方法,比如:遍历、搜索等方法。

以下这个 Sample 就是基本用法的测试:

先把 数据存储到 datum 中,随后输出 相应的 XML 和 JSON 到 std::cout 上。最后再从 JSON Stream 中解析输入到 ptParse 中获得相应 的数据。

[cpp] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. #include <iostream>  
  4. #include <sstream>  
  5. #include <string>  
  6. #include <locale>  
  7.   
  8. #include "boost/property_tree/ptree.hpp"  
  9. #include "boost/property_tree/json_parser.hpp"  
  10. #include "boost/property_tree/xml_parser.hpp"  
  11.   
  12. int main(int argc, char **argv)  
  13. {  
  14.     /* The data format 
  15.      * <root> 
  16.      *  <num>1</num> 
  17.      *  <str>Test</str> 
  18.      * </root> 
  19.      */  
  20.     try  
  21.     {  
  22.         /* create the property tree */  
  23.         boost::property_tree::ptree datum;  
  24.         datum.put("root.num", 100);  
  25.         datum.put("root.str""string");  
  26.   
  27.         /* output XML string */  
  28.         std::ostringstream xmlOutputStream;  
  29.         boost::property_tree::xml_parser::write_xml(xmlOutputStream,  
  30.             datum);  
  31.         std::cout << "XML format:" << std::endl;  
  32.         std::cout << xmlOutputStream.str() << std::endl;  
  33.   
  34.         /* output JSON string */  
  35.         std::ostringstream jsonOutputStream;  
  36.         boost::property_tree::json_parser::write_json(jsonOutputStream,  
  37.             datum);  
  38.         std::cout << "JSON format:" << std::endl;  
  39.         std::cout << jsonOutputStream.str() << std::endl;  
  40.   
  41.         /* read datum from JSON stream */  
  42.         boost::property_tree::ptree ptParse;  
  43.         std::istringstream jsonIStream;  
  44.         jsonIStream.str(jsonOutputStream.str());  
  45.         boost::property_tree::json_parser::read_json(jsonIStream,  
  46.             ptParse);  
  47.         int num = ptParse.get<int>("root.num");  
  48.         std::string strVal = ptParse.get<std::string>("root.str");  
  49.         std::cout << "Num=" << std::dec << num  
  50.             << " Str=" << strVal << std::endl << std::endl;  
  51.     }  
  52.     catch (...)  
  53.     {  
  54.         printf("create boost::property_tree::ptree failed\n");  
  55.     }  
  56.   
  57.     return 0;  
  58. }  



  • 关于字符集

Boost 目前是支持 UTF8 的,但是不能用 直接用 Unicode。所以,如果要存储宽字符就有点麻烦需要用到 Boost 提供的 utf8_codecvt_facet 做转换。

下面就是一个存储 wchar_t 的 Sample:

和之前的其实差不多,有 2 点主要不同。一是用了 wptree 替换了 ptree。二是增加了 utf8_codecvt_facet 在相应的 Stream 里做转换。

[cpp] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. #include <iostream>  
  4. #include <sstream>  
  5. #include <string>  
  6. #include <locale>  
  7.   
  8. #include "boost/property_tree/ptree.hpp"  
  9. #include "boost/property_tree/json_parser.hpp"  
  10. #include "boost/property_tree/xml_parser.hpp"  
  11.   
  12. #include "boost/program_options/detail/convert.hpp"  
  13. #include "boost/program_options/detail/utf8_codecvt_facet.hpp"  
  14.   
  15. int main(int argc, char **argv)  
  16. {  
  17.     /* The data format 
  18.      * <root> 
  19.      *  <num>1</num> 
  20.      *  <str>Test</str> 
  21.      * </root> 
  22.      */  
  23.     /* test UTF-8 format */  
  24.     try  
  25.     {  
  26.         /* create boost utf8 codecvt */  
  27.         std::locale oldLocale;  
  28.         std::locale utf8Locale(oldLocale,  
  29.             new boost::program_options::detail::utf8_codecvt_facet());  
  30.         std::wcout.imbue(utf8Locale);  
  31.   
  32.         /* create the wptree for save the UTF-8 data */  
  33.         boost::property_tree::wptree datum;  
  34.         datum.put(L"root.num", 100);  
  35.         datum.put(L"root.str", L"wstring");  
  36.   
  37.         /* output XML string */  
  38.         std::wostringstream xmlOutputStream;  
  39.         xmlOutputStream.imbue(utf8Locale);  
  40.         boost::property_tree::xml_parser::write_xml(xmlOutputStream,  
  41.             datum);  
  42.         std::wcout << L"XML format:" << std::endl;  
  43.         std::wcout << xmlOutputStream.str() << std::endl;  
  44.   
  45.         /* output JSON string */  
  46.         std::wostringstream jsonOutputStream;  
  47.         jsonOutputStream.imbue(utf8Locale);  
  48.         boost::property_tree::json_parser::write_json(jsonOutputStream,  
  49.             datum);  
  50.         std::wcout << L"JSON format:" << std::endl;  
  51.         std::wcout << jsonOutputStream.str() << std::endl;  
  52.   
  53.         /* read datum from JSON stream */  
  54.         boost::property_tree::wptree wptParse;  
  55.         std::wistringstream jsonIStream;  
  56.         jsonIStream.imbue(utf8Locale);  
  57.         jsonIStream.str(jsonOutputStream.str());  
  58.         boost::property_tree::json_parser::read_json(jsonIStream,  
  59.             wptParse);  
  60.         int num = wptParse.get<int>(L"root.num");  
  61.         std::wstring wstrVal = wptParse.get<std::wstring>(L"root.str");  
  62.         std::wcout << L"Num=" << std::dec << num  
  63.             << L" Str=" << wstrVal << std::endl << std::endl;  
  64.     }  
  65.     catch (...)  
  66.     {  
  67.         printf("create boost::property_tree::wptree failed\n");  
  68.     }  
  69.   
  70.     return 0;  
  71. }