boost ptree操作XML,方便又好用

XML格式检验

在正式开始之前,你最好记下这个自动检查XML格式是否合法的网址:http://tool.oschina.net/codeformat/xml点击打开链接

因为格式错误的配置文件会让程序崩溃,我好像没找到这个库会释放异常,所以这种格式检验的事情还是交给工具去处理吧。

XML格式介绍

另外一个介绍XML格式的,你也最好先看看:http://wenku.baidu.com/link?url=3_sPWvTNPCjq8Q0Udjo-HKQ3lGDoyuY6vc8Z00cyMJgm4ludoT_Ry7LQA9CfFyYeWDOLxMqO_uH16DZ_e3DT0Fygb_v6-7NpTicjuxmrMxO点击打开链接

 Boost ptree

这个类可以解析和操作xml文件。下面的程序就列举和展示ptree类对xml文件的常用操作。

get<type>(path) 获取路径上的节点的属性或者文本内容等

例如:

读取固定路径下单一值

获取文本内容:pt.get<string>("confi.theme");//<theme>this is the result</theme>

获取当前节点的文本内容:pt.get<string>();//<theme>this is the result</theme>,当且仅当当前节点时theme

获取注释内容:pt.get<string("conf.<xmlcomment>");//<conf><!-- this is the result --></conf>

获取属性内容:pt.get<long>("conf.theme.<xmlattr>.id");//<theme id="123456"></theme>,id is 123456

配置文件

<root>
    <students>
        <name>zhang san</name>
        <age>23</age>
    </students>
</root>

代码
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

using namespace std;
using namespace boost::property_tree;

int main()
{
	ptree pt;
	//open xml and read information to pt
	read_xml("conf.xml", pt);
	//read value to val need a path 
	string name = pt.get<string>("root.students.name");
	cout<<"name:"<<name<<endl;
	int age =pt.get<int>("root.students.age");
	cout<<"age:"<<age<<endl;
	return 0;
}
输出
name:zhang san
age:23
请按任意键继续. . .

遍历单层孩子

当迭代器遍历到一个既不是属性节点<xmlattr>也不是一个注释节点<xmlcomment>的时候,此时就是一个子节点,子节点的操作就可以按照:“读取固定路径下单一值”来操作。

遍历孩子使用:auto child = pt.get_child("conf.urls");//获取urls的孩子节点,这里的孩子既包括urls的属性,也包括urls的注释、也包括urls里面的多个子节点<url>www.baidu.com</url><url>www.sina.com</url>,当孩子是一个url节点的时候,迭代器的first是节点的名字url,second是ptree结构,可以继续使用get<type>()方法获取url的属性值,注释值,文本值,或者是子孩子的值。
配置文件
<span style="font-size:14px;"><root>
    <students>
        <name>张三</name>
        <name>李四</name>
	<name>王二</name>
    </students>
</root></span>
源代码
<span style="font-size:14px;">#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

using namespace std;
using namespace boost::property_tree;

int main()
{
	ptree pt;
	//open xml and read information to pt
	read_xml("conf.xml", pt);
	//iter all child value
	auto child = pt.get_child("root.students");
	for (auto i = child.begin();i!=child.end();++i)
	{
		string name = i->second.get_value<string>();//此时i->first的值为路径名:name
		cout<<name<<endl;
	}
	
	return 0;
}</span>
输出

张三
李四
王二
请按任意键继续. . .

遍历包含属性的孩子

配置文件
<root>
    <student name="张三" age="22">first student</student>
    <student name="李四" age="23">second student</student>
    <student name="王二" age="24">third student</student>
</root>

源代码
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

using namespace std;
using namespace boost::property_tree;

int main()
{
	ptree pt;
	//open xml and read information to pt
	read_xml("conf.xml", pt);
	//iter all child value
	auto child = pt.get_child("root");
	for (auto i = child.begin();i!=child.end();++i)
	{
		string stu= i->second.get<string>("");
		cout<<"student:"<<stu<<endl;
		string  name = i->second.get<string>("<xmlattr>.name");
		cout<<"name:"<<name<<endl;
		string  age = i->second.get<string>("<xmlattr>.age");
		cout<<"age:"<<age<<endl;
	}
	return 0;
}


输出
student:first student
name:张三
age:22
student:second student
name:李四
age:23
student:third student
name:王二
age:24
请按任意键继续. . .

嵌套遍历

转自:http://www.verydemo.com/demo_c441_i198847.html

 

摘要:

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. {   
  6.     ptree pt;   
  7.     read_XXX("./test.XXX", pt); // 读文件  
  8.     // ....其他操作   
  9.     write_XXX(cout, pt);        // 写文件,有两种格式:  
  10.                     // void write_XXX(const string &, Ptree &pt);  
  11.                     // void write_XXX(basic_ostream &, Ptree &pt);  
  12. }  
#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.  

    <?xml version="1.0" encoding="utf-8"?>
    
    <config> 
      <file title="windows" size="10Mb"> 
        <!-- File Fisrt Comment -->  
        <!-- File Second Comment -->  
        <paths attr="directory1"> 
          <!-- Paths Comment -->  
          <pathname title="123">abc</pathname>  
          <pathname title="456">efg</pathname>  
          <pathname title="789">hij</pathname> 
        </paths>  
        <paths attr="directory2"> 
          <pathname title="111">klm<!-- pathname Comment -->  
          </pathname>  
          <pathname title="222">nop</pathname>  
          <pathname title="333">qrs</pathname> 
        </paths> 
      </file> 
    </config>
    

    测试代码:

#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[] = "./test.xml";

    string strTmp;

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

    BOOST_AUTO(file_childs, pt.get_child("config.file"));
    //serch(child,0);
    for (BOOST_AUTO(file_childs_iter, file_childs.begin()); file_childs_iter != file_childs.end(); ++file_childs_iter)//file
    {
        strTmp.clear();
        if ("<xmlattr>" == file_childs_iter->first)
        {
            //此节点的first是xmlattr,second节点时pair,按照key,value来取值,key是路径
            strTmp = file_childs_iter->second.get<string>("title");		// 输出:windows
            cout<<file_childs_iter->first<<",  title: "<<strTmp<<"\n";

            strTmp = file_childs_iter->second.get<string>("size");		// 输出:10Mb
            cout<<file_childs_iter->first<<",  size: "<<strTmp<<"\n";

            strTmp = file_childs_iter->second.get<string>("not exits", "This is default");	
            cout<<file_childs_iter->first<<",  not exist:"<<strTmp<<endl;  // 输出:This is default
        }
        else if ("<xmlcomment>" == file_childs_iter->first)
        {
            strTmp = file_childs_iter->second.get_value<string>();		// 第一次输出:File First Comment
            cout<<file_childs_iter->first<<",  comment: "<<strTmp<<"\n";		// 第二次输出:File Second Comment
        }
        else//paths
        {
            BOOST_AUTO(paths_childs, file_childs_iter->second.get_child(""));
            for (BOOST_AUTO(paths_childs_iter, paths_childs.begin()); paths_childs_iter != paths_childs.end(); ++paths_childs_iter)//paths
            {
                strTmp.clear();
                if ("<xmlattr>" == paths_childs_iter->first)
                {
                    cout<<file_childs_iter->first<<" ";
                    //此节点的first是xmlattr,second节点时pair,按照key,value来取值,key是路径
                    strTmp = paths_childs_iter->second.get<string>("attr");
                    cout<<paths_childs_iter->first<<",  attr: "<<strTmp<<"\n";
                }
                else if ("<xmlcomment>" == paths_childs_iter->first)
                {
                    cout<<file_childs_iter->first<<" ";
                    strTmp = paths_childs_iter->second.get_value<string>();		
                    cout<<paths_childs_iter->first<<",  comment: "<<strTmp<<"\n";	
                }
                else//pathname
                {
                        cout<<file_childs_iter->first<<" ";
                        strTmp = paths_childs_iter->second.get<string>("<xmlattr>.title");		
                        cout<<paths_childs_iter->first<<"  title: "<<strTmp<<" content:" <<paths_childs_iter->second.data()<<"\n";	
                }
            }
        }

    }
    cin.get();
    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()”;或者iter->second.get_value<string>()
③数据子节点:这种节点又可以重新看做是一个节点,下面会对应属性子节点、注释子节点、数据子节点。但注意“pos->second.get_child("")”是返回当前节点的所有子节点(包含属性、注释、数据),而“pt.get_child("config.file")“是返回file节点下所有节点(包含属性、注释、数据)。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值