boost 之property_tree操作xml文件
来源:互联网 发布:js对象深拷贝 编辑:程序博客网 时间:2024/06/04 18:06
boost中提供了对配置文件读取的支持,它就是:property_tree。
basic_ptree 是property_tree的核心基础。其接口像std::list。可以执行很多基本的元素操作,比如使用begin()、end()等。
basic_ptree 是property_tree的核心基础。其接口像std::list。可以执行很多基本的元素操作,比如使用begin()、end()等。
此外还加入了操作属性树的get()、get_child()、get_value()、data()等额外的操作。
以下主要对xml配置文件进行读写操作
一.xml文件读操作
接口:
read_xml
实例:
ptree pt;
try
{
read_xml("conf.xml", pt);
}
catch (const std::exception&)
{
cout << "file open err" << endl;
return 0;
}
try
{
read_xml("conf.xml", pt);
}
catch (const std::exception&)
{
cout << "file open err" << endl;
return 0;
}
auto data = pt.get_child("conf");
int gui = data.get<int>("gui");
string theme = data.get<string>("theme");
string url1 = data.get<string>("urls.url");
int style = data.get<int>("clock_style");
int gui = data.get<int>("gui");
string theme = data.get<string>("theme");
string url1 = data.get<string>("urls.url");
int style = data.get<int>("clock_style");
二.xml写操作
接口:
write_xml
实例:
ptree pt2;
try
{
read_xml("conf.xml", pt2);
}
catch (const std::exception&)
{
cout << "file open err" << endl;
return 0;
}
try
{
read_xml("conf.xml", pt2);
}
catch (const std::exception&)
{
cout << "file open err" << endl;
return 0;
}
pt2.put("conf.gui", 88);
write_xml("conf.xml", pt2);//输出数据到文件中
write_xml("conf.xml", pt2);//输出数据到文件中
write_xml(cout, pt2);//输出数据到控制台
/* 不能用注释中的方式写入数据,否则无效
auto writedata = pt2.get_child("conf");
writedata.put("gui", 12);
writedata.put("theme", "aaaaa");
writedata.put("urls.url", "www.baidu.com");
write_xml("conf.xml", pt2);
*/
auto writedata = pt2.get_child("conf");
writedata.put("gui", 12);
writedata.put("theme", "aaaaa");
writedata.put("urls.url", "www.baidu.com");
write_xml("conf.xml", pt2);
*/
xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<conf>
<conf>
<gui>88</gui><theme>matrix</theme><urls>
<url>www.url1.com</url><url>www.url2.com</url><url>www.url3.com</url></urls><clock_style>24</clock_style></conf>
<url>www.url1.com</url><url>www.url2.com</url><url>www.url3.com</url></urls><clock_style>24</clock_style></conf>
阅读全文
0 0
- boost 之property_tree操作xml文件
- boost库之property_tree解析xml文件
- boost使用property_tree操作xml
- boost::property_tree读取解析.xml文件
- boost库用property_tree读xml文件
- BOOST之property_tree 及解析XML详解
- BOOST之property_tree 及解析XML详解
- c++用boost的property_tree库来操作xml配置文件
- BOOST之property_tree对XML修改和保存
- boost property_tree 解析json文件
- Boost学习笔记(四)---利用boost::property_tree;读写xml文件,保存文件
- 使用boost property_tree 生成XML配置文件
- boost.property_tree解析xml的帮助类
- boost中property_tree操作内存中的数据
- boost::property_tree
- boost::property_tree
- boost::property_tree
- boost::property_tree
- viewpage轮播
- 动态规划问题探究及其Java实现
- 摄像机坐标系
- dubbo——广播方式和zoomkeeper方式
- C语言的整型溢出问题 int、long、long long取值范围 最大最小值
- boost 之property_tree操作xml文件
- 用MMDrawerController实现侧滑
- 日历考勤记录
- SpirngMVC异常处理
- android tablelayout嵌套tablayout Fragment嵌套Fragment
- SpringBoot使用thymeleaf的简单记录
- [leetcode]327. Count of Range Sum
- 暑假第22天总结
- 九、 通用工具 ----pair 和Tuple---pair