需要的源码"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();
}