Skip to content
Search

Boost:PropertyTree

lastmod 2018-08-17created 2015-03-1810 edits

The Property Tree library provides a data structure that stores an arbitrarily deeply nested tree of values, indexed at each level by some key. Each node of the tree stores its own value, plus an ordered list of its subnodes and their keys. The tree allows easy access to any of its nodes by means of a path, which is a concatenation of multiple keys.

Category

How to Populate a Property Tree

  • XML Parser
  • JSON Parser
  • INI Parser
  • INFO Parser

XML Example

The strings used to obtain properties are as follows.

#include <boost/property_tree/xml_parser.hpp>
// ...
using namespace boost::property_tree::xml_parser;
using str = std::string;
// ...
str const & decl    = xmldecl   <str>();
str const & attr    = xmlattr   <str>();
str const & comment = xmlcomment<str>();
str const & text    = xmltext   <str>();

INI Example

A sample that reads an INI file is shown below. First, given the following ini file,

[Section]
Value1 = text_string
Value2 = 10

use it as follows.

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
    // ptree 선언
    boost::property_tree::ptree pt;

    // 파일 읽어오기 
    boost::property_tree::ini_parser::read_ini( "test.ini", pt );

    // 문자열 읽기
    std::string value1 = pt.get<std::string>("Section.Value1");

    // 숫자 읽기 
    int value2 = pt.get<int>("Section.Value2");

    return 0;
}

JSON Example

Assume the following JSON.

"property" : [
    {
        "symbolname" : "CLobbyServer#1"
    },
    {
        "symbolname" : "CLobbyServer#1"
    }
]

It can be read as follows.

boost::property_tree::read_json("filename", props);
ptree &children = props.get_child("property");
BOOST_FOREACH(ptree::value_type &vt, children)
{
    const string name = vt.second.get<string>("symbolname");
    // ...
}

See also

Favorite site