<boost> xml文件解析 例4

参考:https://blog.csdn.net/calmreason/article/details/20908551?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control&dist_request_id=1328679.53141.16163987317552173&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.control

XML格式检验
在正式开始之前,你最好记下这个自动检查XML格式是否合法的网址:https://tool.oschina.net/codeformat/xml
因为格式错误的配置文件会让程序崩溃,我好像没找到这个库会释放异常,所以这种格式检验的事情还是交给工具去处理吧。

Boost ptree
这个类可以解析和操作xml文件。下面的程序就列举和展示ptree类对xml文件的常用操作。
get(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

遍历单层孩子

当迭代器遍历到一个既不是属性节点也不是一个注释节点的时候,此时就是一个子节点,子节点的操作就可以按照:“读取固定路径下单一值”来操作。
遍历孩子使用:auto child = pt.get_child("conf.urls");//获取urls的孩子节点,这里的孩子既包括urls的属性,也包括urls的注释、也包括urls里面的多个子节点www.baidu.comwww.sina.com,当孩子是一个url节点的时候,迭代器的first是节点的名字url,second是ptree结构,可以继续使用get()方法获取url的属性值,注释值,文本值,或者是子孩子的值。

配置文件

<?xml version="1.0" encoding="utf-8"?>

<span style="font-size:14px;">
  <root> 
    <students> 
      <name>张三</name>  
      <name>李四</name>  
      <name>王二</name> 
    </students> 
  </root>
</span>

源代码

#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;
}

输出

张三
李四
王二

遍历包含属性的孩子

配置文件

<?xml version="1.0" encoding="utf-8"?>

<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

嵌套遍历

摘要:
property_tree是一个保存了多个属性值的属性数据结构,可以用类似路径的简单方式访问任意节点的属性,而且每个节点都可以用类似STL的风格遍历子节点。property_tree特别适合于应用程序的配置数据处理,可以解析xml、ini、json和info四个格式的文本数据。
在处理四种格式的文件时,除包含头文件、读文件、写文件时有部分区别外,其他对文件内部数据操作时基本一致(因为文件格式都基本一直)。实际上,property_tree内部使用的就是一个小巧快速的开源XML解析器——rapidxml。

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

#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

<?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>  
      <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;
}

执行:

[root@localhost 05_xml_parse]# ./a.out 
<xmlattr>,  title: windows
<xmlattr>,  size: 10Mb
<xmlattr>,  not exist:This is default
<xmlcomment>,  comment:  File Fisrt Comment 
<xmlcomment>,  comment:  File Second Comment 
paths <xmlattr>,  attr: directory1
paths <xmlcomment>,  comment:  Paths Comment 
paths pathname  title: 123 content:abc
paths pathname  title: 456 content:efg
paths pathname  title: 789 content:hij
paths <xmlattr>,  attr: directory2
paths pathname  title: 111 content:klm
paths pathname  title: 222 content:nop
paths pathname  title: 333 content:qrs

分析:
从上述测试中可以看出,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节点下所有节点(包含属性、注释、数据)。

posted @ 2021-03-22 23:19  kouei_kou  阅读(237)  评论(0)    收藏  举报