当前位置:网站首页>初学XML

初学XML

2022-07-07 10:45:00 郑7013

#include"XmlDocument.h"
#include<Windows.h>
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)
    {
        std::cout << "load xml failed!" << std::endl;
    }
    CXmlNode root;
    CXmlNode face;
    ret = doc.SelectSingleNode(_T("/faceconfig"), root);    
    if (ret)
    {
        std::cout << "row:"<<root.GetAttributeInt(_T("row")) << std::endl;
        std::cout << "col:" << root.GetAttributeInt(_T("col")) << std::endl;
        std::cout << "item_width:" << root.GetAttributeInt(_T("item_width")) << std::endl;
        std::cout << "item_height:" << root.GetAttributeInt(_T("item_height")) << std::endl;
        std::cout << "zoom_width:" << root.GetAttributeInt(_T("zoom_width")) << std::endl;
        std::cout << "zoom_height:" << root.GetAttributeInt(_T("zoom_height")) << std::endl;
        ret = root.GetFirstChildNode(_T("face"),face);  
        while (ret)
        {
            std::cout << "[id]:"<< face.GetAttributeInt(_T("id")) << std::endl;
            std::cout << "[tip]:"<< toString(face.GetAttribute(_T("tip"))) << std::endl;
            std::cout << "[file]:"<< toString(face.GetAttribute(_T("file"))) << std::endl;
            ret = face.GetNextSiblingNode(face);        
        }
    }
    face.Release();
    root.Release();
    doc.Release();
    CoUninitialize();
}

原网站

版权声明
本文为[郑7013]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_59393573/article/details/125648656