C++使用Boost库处理XML文件(1)

所用库为boost_1_62_0,IDE为Visual Studio 2013,win7/8系统。

导入头文件(这个使用比较方便,可以先设置文件夹包含位置再导入,也可以直接include文件绝对路径,boost中有些库使用,还要编译,比较麻烦,花了很多时间,还是用不了。)


#include <iostream>
using std::cout;
using std::endl;
using std::string;

#include <boost/property_tree/ptree.hpp>//ptree
using boost::property_tree::ptree;
#include <boost/property_tree/xml_parser.hpp>//read,write
#include <boost/typeof/typeof.hpp>//BOOST_AUTO

目标XML文件,info.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 请不要编辑此文件 -->
<localinfo>
	<player_info>
		<userInfo account="1990wyb" isLastLogin="0" isSave="0"/>
		<userInfo account="tt01234" isLastLogin="1" isSave="1" pass="272E7B02A23578284113B15BA99BA770"/>
		<userInfo account="tietang" isLastLogin="0" isSave="1" pass="2D1A3AAC3243AC9FAA9CE1B6A2286FE8"/>
		<userInfo account="t93" isLastLogin="0" isSave="1" pass="72977BE688F2C369524076B5E9FAA184"/>
	</player_info>
	<setting sound="1" soundVol="3" lagFrame="0" videoEnhance="1" inputEnhance2="1" fullscreenMode="1" fullscreenScale="1" replayDelayFrame="60" win7FixStutter="1" pspvideo="0" vsync="0" triplebuffer="1" displaypowermode="1" additionalSendInterval="0" fullWindowType="1" hintDisable="0" videoD3D="0" joyStickSensitivity="4" noFocusGetInput="1" emuCoinKeyCode="8" emuFCSelectKeyCode="4" vip_enter_room_announcement="0"/>
	<server id="0"/>
	<skin id="0"/>
	<yyinfo keydownid="146714688" speakmode="3" speaklevel="0" volume="100" microphone="100" audioonoff="0"/>
	<bosskey id="0"/>
	<psp cpu_clock="222"/>
	<resolution horizontal="1" vertical="1"/>
	<sound_warning join_game="1"/>
	<PopupMessages>
		<MSGID_NOUPDATE>无可用更新</MSGID_NOUPDATE>
		<MSGID_UPDATEAVAILABLE>发现一个新版本,是否要下载?</MSGID_UPDATEAVAILABLE>
		<MSGID_DOWNLOADSTOPPED>下载被用户中断,更新中止。</MSGID_DOWNLOADSTOPPED>
		<MSGID_CLOSEAPP> 已经打开。需要关闭才能安装。是否继续?</MSGID_CLOSEAPP>
		<MSGID_ABORTORNOT>是否停止下载更新?</MSGID_ABORTORNOT>
		<!-- Optional.
				This is the title to display on the messagebox title bar.
				If extraCmd is set (parameter is presentand the value is not empty), the 3rd button will be appear.
				extraCmd (with its wParam and lParam) will be sent to the handle of application indicated in ClassName2Close
		-->
		<MessageBoxTitle extraCmd="" ecWparam="" ecLparam="">xzone update</MessageBoxTitle>
		</PopupMessages>
</localinfo>
    ptree pt;
    /*读取,去掉空格*/
	read_xml("info.xml", pt, boost::property_tree::xml_parser::trim_whitespace, std::locale());

	/*遍历节点*/
	cout << "server" << endl;
	cout << pt.get_child("localinfo.server").get<int>("<xmlattr>.id", 9) << endl;//如果没有id或者id的值为空,就返回9

	BOOST_AUTO(child, pt.get_child("localinfo"));//获取根节点的子节点
	BOOST_AUTO(pos, child.begin());
	for (; pos != child.end(); ++pos)//循环遍历
	{
		if ("<xmlattr>" == pos->first)//xmlattr
		{
			cout << "<xmlattr>:" << pos->first << endl;
		}
		else if ("<xmlcomment>" == pos->first)//注释
		{
			cout << "<xmlcomment>:"<< pos->first << endl;
			cout << "content:" << pos->second.data() << endl;
		}
		else if("player_info" == pos->first)//player_info
		{
			cout << pos->first << endl;
			BOOST_AUTO(nodes, pos->second.get_child(""));
			BOOST_AUTO(node,nodes.begin());
			for (; node!=nodes.end(); ++node)
			{
				if ("<xmlattr>" == node->first)
				{
					cout << "<xmlattr>:" << node->first << endl;
				}
				else if ("<xmlcomment>" == node->first)
				{
				}
				else
				{
					cout << "\t" << node->first << endl;//节点名称
					//节点属性的值
					cout << "\t\t" << node->second.get<string>("<xmlattr>.account") << endl;
					cout << "\t\t" << node->second.get<int>("<xmlattr>.isLastLogin") << endl;
					cout << "\t\t" << node->second.get<int>("<xmlattr>.isSave") << endl;
					cout << "\t\t" << node->second.get<string>("<xmlattr>.pass","none") << endl;
				}
			}	
		}
		else if ("PopupMessages" == pos->first)//PopupMessages
		{
			cout << pos->first << endl;
			BOOST_AUTO(nodes, pos->second.get_child(""));
			BOOST_AUTO(node, nodes.begin());
			for (; node != nodes.end(); ++node)
			{
				if ("<xmlattr>" == node->first)
				{
					cout << "<xmlattr>:" << node->first << endl;
				}
				else if ("<xmlcomment>" == node->first)
				{
					cout << "\t<xmlcomment>:" << node->first << endl;
					cout << "\t" << node->second.data() << endl;
				}
				else
				{
					cout << "\t" << node->first << endl;//节点名称
					cout << "\t\t" << node->second.data() << endl;//节点值
				}
			}
		}
	}

运行结果:


boost简介:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值