BOOST property_tree

##################################################

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

//read_xml() input xml file
void read_xml(const string &, ptree &pt);
void read_xml(basic_istream &, ptree &pt);

//write_xml() output xml file
void write_xml(const string &, ptree &pt);
void write_xml(basic_ostream &, ptree &pt);

//mark rapidxml (learn it)

##################################################

#include < iostream >
#include < string >
#include < assert.h >
#include < boost/assign.hpp >
#include < boost/typeof/typeof.hpp >
#include < boost/property_tree/ptree.hpp >
#include < boost/property_tree/xml_parser.hpp >
#include < boost/property_tree/json_parser.hpp >
using namespace std;
using namespace boost;
using namespace boost::property_tree;

int main()
{
    {
        ptree pt;
        read_xml("conf.xml", pt);

        cout << pt.get< string >("conf.theme") << endl;
        cout << pt.get< int >("conf.gui") << endl;

        BOOST_AUTO(child, pt.get_child("conf.urls"));
        for(BOOST_AUTO(it, child.begin()); it != child.end(); ++it)
        {
            cout < < it->first << " " << it->second.get_value() << endl;
        }
    }
    cout << endl;

    {
        ptree pt;
        read_xml("conf.xml", pt);
        pt.put("conf.theme", "Matrix Reloaded");
        pt.put("conf.clock_style", 12);
        pt.put("conf.gui", 10);
        pt.add("conf.urls.url", "http://www.url4.org");
        pt.add("conf.block", 10);

        write_xml("out.xml", pt);
    }
    cout << endl;

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

        ptree::assoc_iterator it = pt.find("a");
        cout << it->first << " " << it->second.data() << endl;

        cout < < pt.get_optional("c") << endl;
        assert(!pt.get_optional< string >("not_exist"));
    }
    cout << endl;

    {
        ptree pt;
        read_xml("conf.xml", pt);
        cout < < pt.get("conf.") << endl;
        cout < < pt.get("conf.clock_style..name") << endl;
        cout < < pt.get("conf.theme..id") << endl;
        cout < < pt.get("conf.urls.") << endl;
    }
    cout << endl;

    {
        ptree pt;
        read_json("conf.json", pt);

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

        BOOST_AUTO(child, pt.get_child("conf.urls"));
        for(BOOST_AUTO(it, child.begin()); it != child.end(); ++it)
        {
            cout << it->first << " " << it->second.data() << endl;
        }
    }
    cout << endl;

    {
        ptree pt;
        read_xml("conf.xml", pt, xml_parser::no_concat_text |
            xml_parser::no_comments | xml_parser::trim_whitespace);
    }

    return 0;

}



// 其他

class GlobalConfig : public boost::property_tree::ptree
{
public:
    GlobalConfig(const std::string& configFile) {
        try {
            boost::property_tree::xml_parser::read_xml(configFile,
                    *(static_cast<boost::property_tree::ptree*>(this)));
        } catch (...) {
            std::cout << "xml: read config File: " <<
                configFile << " failed!" << std::endl;
        }
    }
};


extern GlobalConfig g_config;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值