boost::property_tree 基本用法
来源:互联网 发布:双色球算法必中6红组合 编辑:程序博客网 时间:2024/05/16 17:53
最近在开发C++时需要解析和设置配置文件XML,用了boost::property_tree ,看了相关资料并做了一下小小的总结:
1.读取单个值
XML:
<root> <students> <name>zhang san</name> <age>23</age> </students></root>C++代码:
#include <boost/property_tree/ptree.hpp>#include <boost/property_tree/xml_parser.hpp>#include <iostream>using namespace std;using namespace boost::property_tree;int main(){ptree pt;//open xml and read information to ptread_xml("conf.xml", pt);//直接利用点运算符进行读取string name = pt.get<string>("root.students.name");cout<<"name:"<<name<<endl;int age =pt.get<int>("root.students.age");cout<<"age:"<<age<<endl;return 0;}
2.遍历孩子
XML:
<pre code_snippet_id="227288" snippet_file_name="blog_20140310_3_6696140" name="code" class="html" style="margin-top: 0px; margin-bottom: 10px; font-weight: bold; line-height: 24.05px; background-color: rgb(255, 255, 255);"><root> <students> <name>张三</name> <name>李四</name><name>王二</name> </students></root>C++源代码:
<pre code_snippet_id="227288" snippet_file_name="blog_20140310_4_1396223" name="code" class="cpp" style="margin-top: 0px; margin-bottom: 10px; font-weight: bold; line-height: 24.05px; background-color: rgb(255, 255, 255);">int main(){ptree pt;read_xml("conf.xml", pt);//获取students孩子进行遍历auto child = pt.get_child("root.students");for (auto i = child.begin();i!=child.end();++i){string name = i->second.get_value<string>();//注意使用i->second.get<string>()
cout<<name<<endl;}return 0;}3.遍历包含孩子的属性
XML:
<root> <students> <student> <student name="张三" age="22">first student</student> </student> <student> <student name="李四" age="23">second student</student> </student> </students></root>C++源代码:
int main()
{
ptree pt;
read_xml("E:/UIMRIS/BRANCHES/uMR_MAIN/MAIN/Features/win32test/XMLFile1.xml", pt);
auto child = pt.get_child("root.students");
for (auto i = child.begin();i!=child.end();++i)
{
auto student = i ->second.get_child(""); //不需要节点号,但引号必须有
for (auto j = student.begin(); j!= student.end(); ++j)
{
string stu= j->second.get<string>("");
cout<<"student:"<<stu<<endl;
string name = j->second.get<string>("<xmlattr>.name");
cout<<"name:"<<name<<endl;
string age = j->second.get<string>("<xmlattr>.age");
cout<<"age:"<<age<<endl;
}
}
getchar();
return 0;
}
0 0
- boost::property_tree 基本用法
- boost::property_tree实例用法
- boost::property_tree实例用法
- boost::property_tree
- boost::property_tree
- boost::property_tree
- boost::property_tree
- boost.property_tree
- BOOST property_tree
- boost.property_tree使用示例
- boost property_tree遍历
- boost::property_tree讲解
- boost库property_tree示例
- boost的property_tree组件
- Boost property_tree 解析配置文件
- boost的property_tree组件
- boost使用property_tree操作xml
- boost property_tree 解析json文件
- PL/SQL developer 通过简易客户端链接oracle数据库
- LeetCode------Same Tree
- cin/cou效率太慢?试一下 ios::sync_with_stdio(false);
- 【补充git使用相关的别名设置】
- XMLHttpRequest cannot load – Origin is not allowed by Access-Control-Allow-Origin.
- boost::property_tree 基本用法
- makefile文件。批处理文件。
- linux基本命令(1)——ls命令
- Java求最大公约数与最小公倍数
- 区块链技术是银行的双刃剑
- 162. Find Peak Element
- 异常的捕获与处理
- hdu 1712 ACboy needs your help
- android AsyncTask介绍