boost::property_tree 的解析xml模块是基于rapidXml的, 以下是两个例子:
例子1:
#include
#include
#include
#include
using namespace std;
void ReadConfig()
{
boost::property_tree::ptree pt;
boost::property_tree::read_xml("del.conf", pt);
filenum = pt.get("root.delfile.filenum");
cout << "filenum: " << filenum << endl;
BOOST_AUTO(child, pt.get_child("root.delfile.paths"));
for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)
{
BOOST_AUTO(child_paths, pos->second.get_child("")); //此处不需要填结点名,但引号不能省.
for (BOOST_AUTO(pos_paths, child_paths.begin()); pos_paths != child_paths.end(); ++pos_paths)
cout << pos_paths->second.data() << endl;
}
}
int main()
{
ReadConfig();
return 0;
}
/*
附录:配置文件del.conf
*/
35
/tmp/tmp0/
0
/tmp/tmp1/
1
/tmp/tmp2/
2
/tmp/tmp3/
3
/tmp/tmp4/
4
23:59
例子2:
#include
#include
#include
#include
#include
using namespace std;
using namespace boost::property_tree;
int main(void){
ptree pt;
read_xml("debug_settings.xml", pt);
//loop for every node under debug
BOOST_FOREACH(ptree::value_type &v1, pt.get_child("debug")){
if(v1.first == ""){ //it's an attribute
//read debug name="debugname"
cout<< "debug name=" << v1.second.get("name") << endl;
}else if(v1.first == "file"){
//read file name="debug.log"
cout << " file name=" << v1.second.get(".name") << endl;
}
else{ // v1.first == "modules"
//get module type
cout<< " module type:" << v1.second.get(".type") << endl;
//loop for every node under modules
BOOST_FOREACH(ptree::value_type &v2, v1.second){
if(v2.first == ""){ //it's an attribute
//this can also get module type
cout<< " module type again:" << v2.second.get("type") << endl;
}
else{
//all the modules have the same structure, so just use data() function.
cout<< " module name:" << v2.second.data() << endl;
}
}//end BOOST_FOREACH
}
}//end BOOST_FOREACH
}
Finance_Internal
Admin_Internal
HR_Internal
Finance_External
Admin_External
HR_External
----------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------
Boost.PropertyTree 应该是 Boost 1.41.0 开始正式加入 Boost 版本的。目前 ( 2010/02/28 ) 能下到的最新版本是 1.42.0。
Boost.PropertyTree 提供了一种结构化的数据存储容器。并且提供了一系列的解释器可以将内存中的结构与具体格式相互转换 (比如: INI, XML, JSON )。
至少可以用在:
基本用法有 2 种场景。第一种是从 Property Tree存储到具体格式。第二种是从具体格式解析到具体的 Property Tree。其他还有一些 Property Tree 操作的方法,比如:遍历、搜索等方法。
以下这个 Sample 就是基本用法的测试:
先把 数据存储到 datum 中,随后输出 相应的 XML 和 JSON 到 std::cout 上。最后再从 JSON Stream 中解析输入到 ptParse 中获得相应 的数据。
#include
#include
#include
#include
#include
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp"
int main(int argc, char **argv)
{
/* The data format
*
* 1
* Test
*
*/
try
{
/* create the property tree */
boost::property_tree::ptree datum;
datum.put("root.num", 100);
datum.put("root.str", "string");
/* output XML string */
std::ostringstream xmlOutputStream;
boost::property_tree::xml_parser::write_xml(xmlOutputStream,
datum);
std::cout << "XML format:" << std::endl;
std::cout << xmlOutputStream.str() << std::endl;
/* output JSON string */
std::ostringstream jsonOutputStream;
boost::property_tree::json_parser::write_json(jsonOutputStream,
datum);
std::cout << "JSON format:" << std::endl;
std::cout << jsonOutputStream.str() << std::endl;
/* read datum from JSON stream */
boost::property_tree::ptree ptParse;
std::istringstream jsonIStream;
jsonIStream.str(jsonOutputStream.str());
boost::property_tree::json_parser::read_json(jsonIStream,
ptParse);
int num = ptParse.get("root.num");
std::string strVal = ptParse.get("root.str");
std::cout << "Num=" << std::dec << num
<< " Str=" << strVal << std::endl << std::endl;
}
catch (...)
{
printf("create boost::property_tree::ptree failed\n");
}
return 0;
}
Boost 目前是支持 UTF8 的,但是不能用 直接用 Unicode。所以,如果要存储宽字符就有点麻烦需要用到 Boost 提供的 utf8_codecvt_facet 做转换。
下面就是一个存储 wchar_t 的 Sample:
和之前的其实差不多,有 2 点主要不同。一是用了 wptree 替换了 ptree。二是增加了 utf8_codecvt_facet 在相应的 Stream 里做转换。
#include
#include
#include
#include
#include
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include "boost/property_tree/xml_parser.hpp"
#include "boost/program_options/detail/convert.hpp"
#include "boost/program_options/detail/utf8_codecvt_facet.hpp"
int main(int argc, char **argv)
{
/* The data format
*
* 1
* Test
*
*/
/* test UTF-8 format */
try
{
/* create boost utf8 codecvt */
std::locale oldLocale;
std::locale utf8Locale(oldLocale,
new boost::program_options::detail::utf8_codecvt_facet());
std::wcout.imbue(utf8Locale);
/* create the wptree for save the UTF-8 data */
boost::property_tree::wptree datum;
datum.put(L"root.num", 100);
datum.put(L"root.str", L"wstring");
/* output XML string */
std::wostringstream xmlOutputStream;
xmlOutputStream.imbue(utf8Locale);
boost::property_tree::xml_parser::write_xml(xmlOutputStream,
datum);
std::wcout << L"XML format:" << std::endl;
std::wcout << xmlOutputStream.str() << std::endl;
/* output JSON string */
std::wostringstream jsonOutputStream;
jsonOutputStream.imbue(utf8Locale);
boost::property_tree::json_parser::write_json(jsonOutputStream,
datum);
std::wcout << L"JSON format:" << std::endl;
std::wcout << jsonOutputStream.str() << std::endl;
/* read datum from JSON stream */
boost::property_tree::wptree wptParse;
std::wistringstream jsonIStream;
jsonIStream.imbue(utf8Locale);
jsonIStream.str(jsonOutputStream.str());
boost::property_tree::json_parser::read_json(jsonIStream,
wptParse);
int num = wptParse.get(L"root.num");
std::wstring wstrVal = wptParse.get(L"root.str");
std::wcout << L"Num=" << std::dec << num
<< L" Str=" << wstrVal << std::endl << std::endl;
}
catch (...)
{
printf("create boost::property_tree::wptree failed\n");
}
return 0;
}