boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明
http://blog.csdn.net/luopeiyuan1990/article/details/9445691
一下是做的两个实例:入门教程,大神见笑,希望能帮助你尽快进入XML的开发之旅。
<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>
代码:(例程引用了狂人山庄的博客,感谢网友)
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>
#include <iostream> using namespace std; /* 相关的xml参考文件请参考del.conf */ void ReadConfig()
{
boost::property_tree::ptree pt; //定义一个存放xml的容器指针
boost::property_tree::read_xml("del.conf", pt); //读入目录下 del.conf文件 入口在pt这个指针
int filenum = pt.get<int>("root.delfile.filenum"); //将 xml文件中, root节点,下一层delfile 下一层的filenum 作为int类型取出,存在在filenum变量中。 cout << "filenum: " << filenum << endl;//不注释了 - - BOOST_AUTO(child, pt.get_child("root.delfile.paths")); //BOOST_AUTO自动获取表达式, 这里定义个一个节点child指针,并将指针指向 root下一层的delfile的下一层的paths
for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)//由于paths节点有多个节点,并且这些节点名称一样,可以用遍历的方法来获取他们,方法见左
{
BOOST_AUTO(child_paths, pos->second.get_child("")); //此处不需要填结点名,但引号不能省.这里是获取该节点下所有子节点的意思,子节点获取后放在child_path这个指针
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;
}
第二个教程。代码可读性不是很高,但是写法用了一个FOREACH,很简洁。
<debug name="debugname">
<file name="debug.log"/>
<modules type="internal">
<module1>Finance_Internal</module1>
<module2>Admin_Internal</module2>
<module3>HR_Internal</module3>
</modules> <modules type="external">
<module>Finance_External1</module>
<module>Admin_External2</module>
<module>HR_External3</module>
</modules>
</debug>
相应代码:
/*
* test2.cpp
*
* Created on: Jul 23, 2013
* Author: leo_luopy
*/
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp> using namespace std;
using namespace boost::property_tree; int main(void)
{
ptree pt; //定义一个容器入口指针
read_xml("debug_settings.xml", pt); BOOST_FOREACH(ptree::value_type &v1, pt.get_child("debug")){ if(v1.first == "<xmlattr>"){ //it's an attribute
//read debug name="debugname"
cout<< "debug name=" << v1.second.get<string>("name") << endl;
}else if(v1.first == "file"){
//read file name="debug.log"
cout << " file name=" << v1.second.get<string>("<xmlattr>.name") << endl;
}
else{ // v1.first == "modules"
//get module type
cout<< " module type:" << v1.second.get<string>("<xmlattr>.type") << endl; //loop for every node under modules
BOOST_FOREACH(ptree::value_type &v2, v1.second){
if(v2.first == "<xmlattr>"){ //it's an attribute
//this can also get module type
cout<< " module type again:" << v2.second.get<string>("type") << endl;
}
else{
//all the modules have the same structure, so just use data() function.
cout<< " module name:" << v2.second.data() << endl;
}
}//end BOOST_FOREACH
}
}//end BOOST_FOREACH
return 0 ;
}