[XML]数据格式操作

需要的源码"XmlDocument.h"和“XmlDocument.cpp"

在准备一份xml格式文件

//xml
#include"XmlDocument.h"
#include<tchar.h>
#include<Windows.h>

//wchar转string
std::string toString(const std::wstring str)
{
	int len = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL); //获取转换后的长度
	std::string ret;
	if (len <= 0) return ret;
	ret.resize(len);
	WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1,(char*)ret.c_str() , len, NULL, NULL);
	return ret;
}

void xmlTest()
{
	CoInitialize(NULL);	//.com调用

	//创建对象并加载文件
	CXmlDocument doc;
	BOOL ret = doc.Load(_T("FaceConfig.xml"));
	if (!ret)
	{
		cout << "load xml failed!" << endl;
		return;
	}

	CXmlNode root;//根节点对象

	ret = doc.SelectSingleNode(_T("/faceconfig"), root);
	/*
	 //查找faceconfig节点
	“/ ”表示从根节点查到
	“//“表示当前节点下的任意节点
	 "@"表示满足属性条件
	*/
	CXmlNode face;//子节点对象
	if (ret)
	{
		//打印节点下的属性
		cout << "root:" << endl;
		cout << "row:" << root.GetAttributeInt(_T("row")) << endl;
		cout << "col:" << root.GetAttributeInt(_T("col")) << endl;
		cout << "item_width:" << root.GetAttributeInt(_T("item_width")) << endl;
		cout << "item_height:" << root.GetAttributeInt(_T("item_height")) << endl;
		cout << "zoom_width:" << root.GetAttributeInt(_T("zoom_width")) << endl;
		cout << "zoom_height:" << root.GetAttributeInt(_T("zoom_height")) << endl;
		cout << "--------------------" << endl;
		ret = root.GetFirstChildNode(_T("face"), face);
		while (ret)
		{
			//打印子节点下的属性
			cout << "id-" << face.GetAttributeInt(_T("id"))<<"   ";	//int
			cout << "tip-" << toString(face.GetAttribute(_T("tip")))<<"   ";	//wchar 转char
			cout << "file-" << toString(face.GetAttribute(_T("file"))) << endl;	//wchar 转char
			ret = face.GetNextSiblingNode(face);	//获取兄弟节点
		}
	}
	face.Release();
	root.Release();
	doc.Release();
	CoUninitialize();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值