Boost:PropertyTree
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 Example
Property를 획득하기 위한 문자는 아래와 같다.
#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
INI파일을 읽는 샘플 예제는 아래와 같다. 우선 아래의 ini파일이 존재했을때,
아래와 같이 사용하면 된다.
#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
아래의 JSON이 있다고 가정한다.
아래와 같이 읽어올 수 있다.
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");
// ...
}