BOOST之property_tree 及解析XML详解

property_tree是一个保存了多个属性值的属性数据结构,可以用类似路径的简单方式访问任意节点的属性,而且每个节点都可以用类似STL的风格遍历子节点。property_tree特别适合于应用程序的配置数据处理,可以解析xml、ini、json和info四个格式的文本数据。

在处理四种格式的文件时,除包含头文件、读文件、写文件时有部分区别外,其他对文件内部数据操作时基本一致(因为文件格式都基本一直)。实际上,property_tree内部使用的就是一个小巧快速的开源XML解析器——rapidxml。


使用方法:

1)不同:(XXX分别代码xml、json、ini、info)

  1. #include <boost/property_tree/ptree.hpp> 
  2. #include <boost/property_tree/XXX_parser.hpp> 
  3. using namespace boost::property_tree; 
  4. void main(void
  5.     ptree pt; 
  6.     read_XXX("./test.XXX", pt); // 读文件 
  7.     // ....其他操作 
  8.     write_XXX(cout, pt);        // 写文件,有两种格式: 
  9.                     // void write_XXX(const string &, Ptree &pt); 
  10.                     // void write_XXX(basic_ostream &, Ptree &pt); 
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/XXX_parser.hpp>
using namespace boost::property_tree;
void main(void)
{
	ptree pt;
	read_XXX("./test.XXX", pt);	// 读文件
	// ....其他操作
	write_XXX(cout, pt);		// 写文件,有两种格式:
					// void write_XXX(const string &, Ptree &pt);
					// void write_XXX(basic_ostream &, Ptree &pt);
}


2)相同:(下面以xml为基础详细介绍,其他三种类型没测试过,囧~)

测试的XML文件:test.xml

  1. <config> 
  2.     <file title="windows" size="10Mb"> 
  3.     <!-- File Fisrt Comment --> 
  4.     <!-- File Second Comment --> 
  5.         <paths attr="directory"> 
  6.         <!-- Paths Comment --> 
  7.             <pathname title="123">abc</pathname> 
  8.             <pathname title="456">efg</pathname> 
  9.             <pathname title="789">hij</pathname> 
  10.         </paths> 
  11.         <paths> 
  12.             <pathname title="111">klm</pathname> 
  13.             <pathname title="222">nop</pathname> 
  14.             <pathname title="333">qrs</pathname> 
  15.         </paths> 
  16.     </file> 
  17. </config> 
<config>
	<file title="windows" size="10Mb">
	<!-- File Fisrt Comment -->
	<!-- File Second Comment -->
		<paths attr="directory">
		<!-- Paths Comment -->
			<pathname title="123">abc</pathname>
			<pathname title="456">efg</pathname>
			<pathname title="789">hij</pathname>
		</paths>
		<paths>
			<pathname title="111">klm</pathname>
			<pathname title="222">nop</pathname>
			<pathname title="333">qrs</pathname>
		</paths>
	</file>
</config>


测试代码:

  1. #include <iostream> 
  2. #include <string> 
  3. #include <boost/typeof/typeof.hpp> 
  4. #include <boost/property_tree/ptree.hpp> 
  5. #include <boost/property_tree/xml_parser.hpp> 
  6.  
  7. using namespace std; 
  8. using namespace boost::property_tree; 
  9.  
  10. int main(void
  11.     char szXmlFile[] = "E:/test.xml"
  12.  
  13.     string strTmp; 
  14.  
  15.     ptree pt; 
  16.     xml_parser::read_xml(szXmlFile, pt); 
  17.  
  18.     BOOST_AUTO(child, pt.get_child("config.file")); 
  19.     for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos) 
  20.     { 
  21.         strTmp.clear(); 
  22.         if ("<xmlattr>" == pos->first) 
  23.         { 
  24.             strTmp = pos->second.get<string>("title");     // 输出:windows 
  25.             cout<<strTmp<<"\t"
  26.  
  27.             strTmp = pos->second.get<string>("size");      // 输出:10Mb 
  28.             cout<<strTmp<<"\t"
  29.  
  30.             strTmp = pos->second.get<string>("noexits", "This is default");     
  31.             cout<<strTmp<<endl;  // 输出:This is default 
  32.         } 
  33.         else if ("<xmlcomment>" == pos->first) 
  34.         { 
  35.             strTmp = pos->second.data();     // 第一次输出:File First Comment 
  36.             cout<<strTmp<<endl;     // 第二次输出:File Second Comment 
  37.         } 
  38.         else 
  39.         { 
  40.             BOOST_AUTO(nextchild, pos->second.get_child("")); 
  41.             for (BOOST_AUTO(nextpos, nextchild.begin()); nextpos != nextchild.end(); ++nextpos) 
  42.             { 
  43.                 strTmp.clear(); 
  44.                 if ("<xmlattr>" == nextpos->first) 
  45.                 { 
  46.                     strTmp = nextpos->second.get<string>("attr");  // 输出:directory 
  47.                     cout<<strTmp<<endl; 
  48.                 } 
  49.                 else if ("<xmlcomment>" == nextpos->first) 
  50.                 { 
  51.                     strTmp = nextpos->second.data();     // 输出:Paths Comment 
  52.                     cout<<strTmp<<endl; 
  53.                 } 
  54.                 else 
  55.                 { 
  56.                     strTmp = nextpos->second.get<string>("<xmlattr>.title"); 
  57.                     cout<<strTmp<<"\t"
  58.  
  59.                     strTmp = nextpos->second.data(); 
  60.                     cout<<strTmp<<endl; 
  61.                 } 
  62.             } 
  63.         } 
  64.          
  65.     } 
  66.     return 0; 
#include <iostream>
#include <string>
#include <boost/typeof/typeof.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;
using namespace boost::property_tree;

int main(void)
{
	char szXmlFile[] = "E:/test.xml";

	string strTmp;

	ptree pt;
	xml_parser::read_xml(szXmlFile, pt);

	BOOST_AUTO(child, pt.get_child("config.file"));
	for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)
	{
		strTmp.clear();
		if ("<xmlattr>" == pos->first)
		{
			strTmp = pos->second.get<string>("title");		// 输出:windows
			cout<<strTmp<<"\t";

			strTmp = pos->second.get<string>("size");		// 输出:10Mb
			cout<<strTmp<<"\t";

			strTmp = pos->second.get<string>("noexits", "This is default");	
			cout<<strTmp<<endl;  // 输出:This is default
		}
		else if ("<xmlcomment>" == pos->first)
		{
			strTmp = pos->second.data();		// 第一次输出:File First Comment
			cout<<strTmp<<endl;		// 第二次输出:File Second Comment
		}
		else
		{
			BOOST_AUTO(nextchild, pos->second.get_child(""));
			for (BOOST_AUTO(nextpos, nextchild.begin()); nextpos != nextchild.end(); ++nextpos)
			{
				strTmp.clear();
				if ("<xmlattr>" == nextpos->first)
				{
					strTmp = nextpos->second.get<string>("attr");	// 输出:directory
					cout<<strTmp<<endl;
				}
				else if ("<xmlcomment>" == nextpos->first)
				{
					strTmp = nextpos->second.data();		// 输出:Paths Comment
					cout<<strTmp<<endl;
				}
				else
				{
					strTmp = nextpos->second.get<string>("<xmlattr>.title");
					cout<<strTmp<<"\t";

					strTmp = nextpos->second.data();
					cout<<strTmp<<endl;
				}
			}
		}
		
	}
	return 0;
}

测试结果:



分析:从上述测试中可以看出,BOOST封装的RapidXml开源库,是将XML文件内容解析为一个树状结构。比如说本例中使用的节点“config.file”,具有五个子节点:一个属性子节点、两个注释子节点、两个数据子节点,且顺序为属性→注释→数据
①属性子节点:
每个节点只有一个属性子节点,是一对多关系,即一个属性子节点对应多个属性!
"if ("<xmlattr>" == pos->first)",然后获取属性的值则为“pos->second.get<string>("title")”和“pos->second.get<string>("size")”。注意这里获取属性,不再是"<xmlattr>.title",因为此时pos已经指向本节点,不再需要用"<xmlattr>"递推到属性子节点!
②注释子节点:节点可以有多个属性子节点,是一对一关系!!!
”if ("<xmlcomment>" == pos->first)“,获取属性则“pos->second.data()”;
③数据子节点:这种节点又可以重新看做是一个节点,下面会对应属性子节点、注释子节点、数据子节点。但注意“pos->second.get_child("")”是返回当前节点的所有子节点(包含属性、注释、数据),而“pt.get_child("config.file")“是返回file节点下所有节点(包含属性、注释、数据)。


参考资料:
1、boost::property_tree


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值