c++11 boost - 容器(5) property_tree

2018-07-12 16:32:26

property_tree 可以解析xml、json、ini、info。

解析 xml

using namespace boost::property_tree;
ptree pt;
pt.get<string>("debug.sex","0");        //如果获取不到则返回"0"
pt.get_child("debug.modules");          //获取子对象
pt.add_child("debug.modules");          //新增子对象

read_xml(string&, ptree&,xml_parser::trim_whitespace);          //不格式化
read_xml(basic_istream&, ptree &);
write_xml(string &, ptree&);
write_xml(basic_ostream &, ptree &);

pt.put("conf.urls.url", "aaa")          //只能新增,修改已有的属性值,不能添加新节点
pt.add("conf.urls.url", "bbb")          //新增新节点

pt.get<string>("debug.<xmlcomment>");           //获取注释
//获取属性
pt.get<string>("debug.file.<xmlattr>.name");
pt.get<int>("debug.file.<xmlattr>.age");

下面是 conf.xml

<conf>
    <!-- conf comment -->
    <gui>1</gui>
    <theme id="001">matrix</theme>
    <urls>
    <!-- urls comment -->
    	<url>http://www.freecls.com</url>
        <url>http://www.url1.com</url>
        <url>http://www.url2.com</url>
    </urls>
    <clock_style name="local">24</clock_style>
</conf>
// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace boost::property_tree;

void case1()
{
    ptree pt;
    read_xml("conf.xml", pt);

    pt.get<string>("conf.theme");
    pt.get<int>("conf.clock_style");
    pt.get("conf.no_prop", 100);  //如果不存在,返回100

    cout << pt.get<string>("conf.theme") << endl;
    cout << pt.get<int>("conf.clock_style") << endl;
    cout << pt.get<long>("conf.gui")<< endl;
    cout << pt.get("conf.no_prop", 100)<< endl;

    //遍历子节点
    auto child = pt.get_child("conf.urls");
    for (auto& x : child){
    	cout <<  x.second.get_value<string>() << ",";   
    }
    cout << endl;

    for(auto& x : pt.get_child("conf.urls"))
    {
        cout <<  x.second.data() << ",";
    }
    cout << endl;

}

//写入配置信息,如果子节点不存在,就相当于新增
void case2()
{
    ptree pt;
    read_xml("conf.xml", pt);

    pt.put("conf.theme", "Matrix Reloaded");
    pt.put("conf.clock_style", 12);
    pt.put("conf.gui", 0);
    //pt.put("conf.urls.url", "http://www.url4.org");
    //加入新节点
    pt.add("conf.urls.url", "http://www.url4.org");

    //写入到流
    write_xml(cout , pt);
}

void case3()
{
    ptree pt;
    read_xml("conf.xml", pt);
    cout << pt.get<string>("conf.<xmlcomment>") << endl;
    cout << pt.get<string>("conf.clock_style.<xmlattr>.name") << endl;
    cout << pt.get<long>("conf.theme.<xmlattr>.id")<< endl;  //获取属性
    cout << pt.get<string>("conf.urls.<xmlcomment>")<< endl; //获取注释

}
int main()
{
    case1();
    case2();
    case3();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
matrix
24
1
100
 urls comment ,http://www.freecls.com,http://www.url1.com,http://www.url2.com,
 urls comment ,http://www.freecls.com,http://www.url1.com,http://www.url2.com,
<?xml version="1.0" encoding="utf-8"?>
<conf>
    
    
    
    
    
<!-- conf comment --><gui>0</gui><theme id="001">Matrix Reloaded</theme><urls>
    
        
        
        
    <!-- urls comment --><url>http://www.freecls.com</url><url>http://www.url1.com</url><url>http://www.url2.com</url><url>http://www.url4.org</url></urls><clock_style name="local">12</clock_style></conf> conf comment 
local
1
 urls comment


解析json

例子-1

#include <boost/property_tree/ptree.hpp>  
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <iostream>
using namespace std;
const std::string file_path = "./test.txt";


//{"user":[{"ID":"1","Name":"wang"},{"ID":"2","Name":"zhang"},{"ID":"3","Name":"li"}],"age":"22"}
void generate_user()
{
    boost::property_tree::ptree root;
    boost::property_tree::ptree items;

    boost::property_tree::ptree item1;
    item1.put("ID", "1");
    item1.put("Name", "wang");
    items.push_back(std::make_pair("", item1));         //""代表数组[]

    boost::property_tree::ptree item2;
    item2.put("ID", "2");
    item2.put("Name", "zhang");
    items.push_back(std::make_pair("", item2));


    boost::property_tree::ptree item3;
    item3.put("ID", "3");
    item3.put("Name", "li");
    items.push_back(std::make_pair("", item3));

    root.put_child("user", items);
    root.put("age", 22);
    
    //写文件
    boost::property_tree::write_json(file_path, root);
    
    //写内存
    std::stringstream iss;
    boost::property_tree::write_json(iss, root);
    std::cout << iss.str() << std::endl;
    
}

void read_user()
{

    std::stringstream iss("{\"user\":[{\"ID\":\"1\",\"Name\":\"wang\"},{\"ID\":\"2\",\"Name\":\"zhang\"},{\"ID\":\"3\",\"Name\":\"li\"}],\"age\":\"22\"}");
    //iss.str("{\"user\":[{\"1\":{\"ID\":\"1\",\"Name\":\"wang\"}}], \"age\":22}");
    boost::property_tree::ptree root;
    boost::property_tree::ptree items;
    boost::property_tree::ptree items_arr;
    boost::property_tree::read_json(iss, root);


    items = root.get_child("user");
    int age = root.get<int>("age");
    std::cout << "age: " << age << std::endl;
    
    BOOST_FOREACH(boost::property_tree::ptree::value_type &v, items)  
    {  
      std::cout << v.second.get<string>("ID") << ":" << v.second.get<string>("Name") << std::endl;  
    }
    
    return;
    
    for (boost::property_tree::ptree::iterator it = items.begin(); it != items.end(); ++it){
        //遍历读出数据
        items_arr = it->second;
        boost::property_tree::ptree::iterator it1 = items.begin();
        
        //数组
        for(it1;it1 != items_arr.end(); it1++){
            //std::cout << it1->first << std::endl;
            
            std::cout << it1->second.get<string>("ID") << std::endl;
            std::cout << it1->second.get<string>("Name") << std::endl;
        }
    }
}
int main()
{
    generate_user();
    read_user();
    
    return 0;

}
[root@192 c++]# g++ -std=c++11 main.cpp -g
[root@192 c++]# 
[root@192 c++]# 
[root@192 c++]# ./a.out 
{
    "user": [
        {
            "ID": "1",
            "Name": "wang"
        },
        {
            "ID": "2",
            "Name": "zhang"
        },
        {
            "ID": "3",
            "Name": "li"
        }
    ],
    "age": "22"
}

age: 22
1:wang
2:zhang
3:li

例子-2

conf.json

{
    "conf":
    {
        "gui": 1,
            "theme": "matrix",
            "urls":
            {
                "url": "http://www.url1.com"
            },
            "clock_style": 24
    }
}
// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/property_tree/ptree.hpp>
using namespace boost::property_tree;

#include <boost/property_tree/json_parser.hpp>
void case4()
{
    ptree pt;
    read_json("conf.json", pt);

    cout << pt.get<string>("conf.theme") << endl;
    cout << pt.get<int>("conf.clock_style") << endl;
    cout << pt.get<long>("conf.gui")<< endl;
    cout << pt.get("conf.no_prop", 100)<< endl; 

    for (auto x : pt.get_child("conf.urls"))
    {   cout <<  x.second.data() << ",";    }

}

int main()
{
    case4();
}
[root@192 c++]# g++ -std=c++11 main.cpp -g
[root@192 c++]# ./a.out 
matrix
24
1
100


解析ini

例子

test.ini

[sec1]
name=freecls
url=http://www.freecls.com

[sec2]
job=it
color=red
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost::property_tree;

int case1(void){
    ptree pt;
    ptree pt2;
    read_ini("test.ini", pt);
    string tmp;

    tmp = pt.get<string>("sec1.name");
    
    pt2 = pt.get_child("sec1");
    
    for(auto &v : pt2){
        cout << v.first << " " << v.second.data() << endl;
    }
    cout << endl;
}


int case2(void){
    ptree pt;
    ptree pt2;
    stringstream iss;

    pt.put("sec1.name", "freecls");
    pt.put("sec1.url", "http://www.freecls.com");
    
    pt.put("sec2.job", "it");
    pt.put("sec2.color", "red");
    
    write_ini(iss, pt);
    cout << iss.str() << endl;
}

int main(){
	case1();
	case2();
}
[root@192 c++]# g++ -std=c++11 main.cpp
[root@192 c++]# ./a.out 
name freecls
url http://www.freecls.com

[sec1]
name=freecls
url=http://www.freecls.com
[sec2]
job=it
color=red


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3..原文地址http://www.freecls.com/a/2712/a9


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。