boost::property_tree 的解析xml模块是基于rapidXml的, 以下是两个例子:
例子1:
- #include <boost/property_tree/ptree.hpp>
- #include <boost/property_tree/xml_parser.hpp>
- #include <boost/typeof/typeof.hpp>
- #include <iostream>
- using namespace std;
- void ReadConfig()
- {
- boost::property_tree::ptree pt;
- boost::property_tree::read_xml("del.conf", pt);
- filenum = pt.get<int>("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
- */
- <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>
- <