// FirstTest.cpp : 定义控制台应用程序的入口点。
//ptree解析xml
#include "stdafx.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>
#include <iostream>
using namespace boost::property_tree;
using namespace std;
void ReadConfig()
{
boost::property_tree::ptree pt;
boost::property_tree::read_xml("del.conf", pt);
int filenum = pt.get<int>("root.delfile.filenum");
cout << "filenum: " << filenum << endl;
BOOST_AUTO(paths, pt.get_child("root.delfile.paths"));
for (BOOST_AUTO(pos, paths.begin()); pos != paths.end(); ++pos)
{
BOOST_AUTO(path, pos->second.get_child("")); //""表示所有
for (BOOST_AUTO(pos_paths, path.begin()); pos_paths != path.end(); ++pos_paths)
cout << pos_paths->second.data() << endl;
}
}
int main()
{
ReadConfig();
system("pause");
return 0;
}
<root>
<delfile>
<filenum> 35 </filenum>
<paths>
<path>
<pathname>/tmp/tmp0/</pathname>
<before_hours> 0 </before_hours>
</path>
<path>
<pathname>/tmp/tmp1/</pathname>
<before_hours> 1 </before_hours>
</path>
<path>
<pathname>/tmp/tmp2/</pathname>
<before_hours> 2 </before_hours>
</path>
<path>
<pathname>/tmp/tmp3/</pathname>
<before_hours> 3 </before_hours>
</path>
<path>
<pathname>/tmp/tmp4/</pathname>
<before_hours> 4 </before_hours>
</path>
</paths>
</delfile>
<backup>
<backuptime> 23:59 </backuptime>
</backup>
</root>