property_tree 是一个保存了多个属性值的树形数据结构,可以用类似路径的简单方式访问任意节点的属性,而且每个节点都可以用类似STL的风格遍历子节点。
property_tree 特别适合于应用程序的配置数据处理,可以解析 xml, ini, json, info 四种格式的文本数据,使用它能够减轻自己开发配置管理的工作。
以 XML 为例:
1
2 /* *
3 * Build Success By VC++ 2010
4 *
5 * boost::property_tree
6 *
7 * copyright (C) 2010, liya
8 */
9
10 /* * Example XML
11 *
12 * <app>
13 * <version>1.0.0.1</version>
14 * <theme>blue</theme>
15 * <about>
16 * <url> http://www.xyz.com </url>
17 * <email>support@xyz.com</email>
18 * <content>coryright (C) xyz.com 2000-2010</content>
19 * </about>
20 * </app>
21 */
22
23 #include < iostream >
24 #include < string >
25 #include < boost / property_tree / ptree.hpp >
26 #include < boost / property_tree / xml_parser.hpp >
27
28 using namespace std;
29 using namespace boost::property_tree;
30
31 void CreateConfig( string filename)
32 {
33 ptree pt;
34 read_xml(filename, pt);
35
36 pt.put( " app.version " , " 1.0.0.1 " );
37 pt.put( " app.theme " , " blue " );
38 pt.put( " app.about.url " , " http://www.xyz.com " );
39 pt.put( " app.about.email " , " support@xyz.com " );
40 pt.put( " app.about.content " , " coryright (C) xyz.com 2000-2010 " );
41
42 write_xml(filename, pt);
43 }
44
45 int main( int argc, char * argv[])
46 {
47 CreateConfig( string ( " config.xml " )); // config.xml 文件必须存在,但可以为空。
48
49 return 0 ;
50 }
51
2 /* *
3 * Build Success By VC++ 2010
4 *
5 * boost::property_tree
6 *
7 * copyright (C) 2010, liya
8 */
9
10 /* * Example XML
11 *
12 * <app>
13 * <version>1.0.0.1</version>
14 * <theme>blue</theme>
15 * <about>
16 * <url> http://www.xyz.com </url>
17 * <email>support@xyz.com</email>
18 * <content>coryright (C) xyz.com 2000-2010</content>
19 * </about>
20 * </app>
21 */
22
23 #include < iostream >
24 #include < string >
25 #include < boost / property_tree / ptree.hpp >
26 #include < boost / property_tree / xml_parser.hpp >
27
28 using namespace std;
29 using namespace boost::property_tree;
30
31 void CreateConfig( string filename)
32 {
33 ptree pt;
34 read_xml(filename, pt);
35
36 pt.put( " app.version " , " 1.0.0.1 " );
37 pt.put( " app.theme " , " blue " );
38 pt.put( " app.about.url " , " http://www.xyz.com " );
39 pt.put( " app.about.email " , " support@xyz.com " );
40 pt.put( " app.about.content " , " coryright (C) xyz.com 2000-2010 " );
41
42 write_xml(filename, pt);
43 }
44
45 int main( int argc, char * argv[])
46 {
47 CreateConfig( string ( " config.xml " )); // config.xml 文件必须存在,但可以为空。
48
49 return 0 ;
50 }
51