boost读取xml文件

本文介绍了如何利用Boost库中的property_tree模块来读取XML文件。主要涉及的类为boost::property_tree::ptree,通过调用read_xml函数,可以接受fstream或文件路径作为参数进行解析。解析完成后,可以通过ptree的get_child和get方法获取XML文件中的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  boost的property_tree可以用来读取xml文件。用到的类 boost::property_tree::ptree,这个是用来保存解析后的xml数据流的类,解析函数read_xml,这个函数支持

  fstream和文件路径两种参数。

  获取xml项就要靠最终保存分析结果的ptree类了。

  若只是读取用到的成员基本就是两个ptree.get_child和ptree.get。

 

  xml文件:

<Games DefaultGame = "利剑1">
	<Group name = "中和游戏">			
	     <child id = "0" name="利剑1"/>
	     <child id = "1" name="利剑2"/>
	</Group>
</Games>

读取代码:

#include <boost/property_tree/ptree.hpp>
#include "boost/property_tree/xml_parser.hpp"
#include <windows.h>

using boost::property_tree::ptree;
int _tmain(int argc, _TCHAR* argv[])
{
	ptree pt;
	read_xml("GamesList.xml", pt); //解析xml文件到ptree

	for (auto& root: pt.get_child("Games"))  //遍历Games的子项
	{
		if (root.first == "<xmlattr>&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值