一点一滴成长

导航

boost-使用property_tree来解析xml、json

property_tree是一个保存了多个属性值的树形数据结构,可以用来解析xml、json、ini、info文件。要使用property_tree和xml解析组件的话需要包含"boost/property_tree/ptree.hpp"和"boost/property_tree/xml_parser.hpp"。我们一般使用property_tree中预定义好的typedef: ptree来处理数据。

1、XML

如以下为读取xml文件中内容的示例:

<?xml version="1.0" encoding="UTF-8"?>
<conf>
    <gui>0</gui>
    <theme>matrix</theme>
    <urls>
        <url>http:://www.url1.com</url>
        <url>http:://www.url2.com</url>
        <url>http:://www.url3.com</url>
        <url></url>
    </urls>
    <clock_style>24.35</clock_style>
</conf>
#include <cstdio>
#include <string>
#include <iostream>
using std::string;
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/xml_parser.hpp"
#include "boost/typeof/typeof.hpp"
#include "boost/optional.hpp"
using namespace boost::property_tree;

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

    boost::optional<int> op = pt.get_optional<int>("conf.gui");//使用get_optional()获得节点,不存在则op为空
    if (op)
    {
        int i = op.get();
    }

    string str1 = pt.get<string>("conf.theme", "");//使用get()获得节点,不存在则返回""

    boost::optional<ptree&> child_null_test = pt.get_child_optional("conf.urls");
    if (child_null_test)
    {
        BOOST_AUTO(child, pt.get_child("conf.urls"));
        for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)
        {
            string str = pos->second.get_value<string>();
            std::cout << str << std::endl;
        }
    }

    string str = pt.get("conf.clock_style", "");
}

以下为向xml文件添加内容:

    ptree pt;
    read_xml("conf.xml", pt);
    pt.add("conf.urls.url", "http://www.url4.com");
    write_xml("conf.xml", pt);

以下为修改xml中内容:

ptree pt;
    read_xml("conf.xml", pt);
    pt.put("conf.gui", 99);
    write_xml("conf.xml", pt);

以下为新建一个xml文件或打开一个xml并将里面的内容清除:

ptree pt;
    pt.put("conf.gui", 0);
    write_xml("conf.xml", pt);

节点的属性保存在节点的<xmlattr>中,节点的注释保存在节点的<xmlcomment>中,获取示例如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<conf><!--conf comment-->
    <gui>1</gui>
    <theme id="001">matrix</theme>
    <urls><!--urls comment-->
    </urls>
</conf>
ptree pt;
    read_xml("conf.xml", pt);
    boost::optional<string> op = pt.get_optional<string>("conf.<xmlcomment>");
    if (op)
    {
        string strCommentConf = op.get();
    }
    op = pt.get_optional<string>("conf.urls.<xmlcomment>");
    if (op)
    {
        string strCommentUrls = op.get();
    }
    op = pt.get_optional<string>("conf.theme.<xmlattr>.id");
    if (op)
    {
        string strAttr = op.get();
    }

 2、JSON

使用property_tree解析json与解析xml的接口方法基本相同,不同的地方是读取json文件使用read_json,写入json文件使用write_json,如下示例:

 

{
    "conf":
    {
        "gui": 1,
        "theme": "matrix",
        "urls":
        {
            "url": "http://www.url1.com",
            "url": "http://www.url2.com",
            "url": "http://www.url3.com"
        },
        "clock_style": 24
    }
}

 

#include <cstdio>
#include <string>
#include <iostream>
using std::string;
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/typeof/typeof.hpp"
#include "boost/optional.hpp"
using namespace boost::property_tree;

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

    boost::optional<int> op = pt.get_optional<int>("conf.gui");//不存在则op为空
    if (op)
    {
        int i = op.get();
        int a = 0;
    }

    string str1 = pt.get<string>("conf.theme", "");

    boost::optional<ptree&> child_null_test = pt.get_child_optional("conf.urls");
    if (child_null_test)
    {
        BOOST_AUTO(child, pt.get_child("conf.urls"));
        for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)
        {
            string str = pos->second.get_value<string>();
            std::cout << str << std::endl;
        }
    }

    string str = pt.get("conf.clock_style", "");
    
    return getchar();
}

 

posted on 2017-08-24 14:24  整鬼专家  阅读(6302)  评论(0编辑  收藏  举报