定义xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<conf>
<gui>96</gui>
<theme>小青峰jd</theme>
<urls>
<url>http://blog.csdn.net/jiangdong2007</url>
<url>http://www.ur22.com</url><
url>http://www.ur33.com</url>
</urls>
<clock_style>24</clock_style><
/conf>
#include <iostream>
#include <string>#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace std;
using namespace boost;
int main(int argc, _TCHAR* argv[])
{
using namespace boost::property_tree;
ptree pt;
read_xml("D:\\test.xml",pt); //文件路径一定要正确,否则程序报错
cout<<pt.get<string>("conf.theme")<<endl; //读出文件内容
cout<<pt.get<int>("conf.clock_style")<<endl;
cout<<pt.get<long>("conf.gui")<<endl;
cout<<pt.get("conf.no_prop",100)<<endl; //没有返回100
BOOST_AUTO(child,pt.get_child("conf.urls"));
for (BOOST_AUTO(pos,child.begin()); pos!= child.end(); ++pos)
{
cout<<pos->second.data()<<"\t";
}
cout<<endl;
pt.put("conf.theme","小青峰jd");//修改
//write_xml(cout,pt); //写入到流
write_xml("d:\\test.xml",pt); //写入到文件
cout<<endl;
//system("pause");
return EXIT_SUCCESS;
}