您的位置:首页 > 编程语言 > C语言/C++

使用MSXML来保存配置信息的CPP类

2008-03-31 15:51 232 查看
程序运行时刻一般需要保存,更改一些配置信息,为了方便,这里使用XML来保存配置信息,它使用了微软的MSXML库,程序也很简单,用XMLConfig.hpp 和XMLConfig.cpp 实现了配置类,在main.cpp文件中放置测试代码。

具体的源码如下:

XMLConfig.hpp 文件 :


#ifndef _XMLCONFIG_HPP_


#define _XMLCONFIG_HPP_




/**//**********************************************************************************************************************


Copyright(C) 2008,soho 保留所有权利 ( All rights reserved. )




文件名称:XMLConfig.hpp


摘 要:XMLConfig类定义头文件, 通过该类可以读写配置项,并用xml格式的文件把配置项记录下来


当前版本:1.0


作 者:snail


创建日期:2008年3月30日


历 史:2008-03-30 - create


2008-03-31 - add method and test it


**********************************************************************************************************************/




#include <atlbase.h>


#include <string>


#include <msxml2.h>






/**//******************************************************************************************


摘要 : 类XMLConfig用来读写xml格式的配置文件


创建日期 : 2008年3月30日


历史 : 2008年3月30日-- created.


******************************************************************************************/


class XMLConfig




...{


public:


XMLConfig();


~XMLConfig();




bool ConstructXMLFile();


bool LoadFromXMLFile(const std::string& fileName);


bool SaveToXMLFile(const std::string& fileName);




void GetXML(std::string& strXML);




bool AppendAttributeNode(const std::string& nodeName,


const std::string& attrName,


const std::string& attrValue);


bool GetAttributeValue( const std::string& nodeName,


const std::string& attrName,


std::string& attrValue);




private:


void _SafeReleaseXMLDoc();




IXMLDOMDocument2* _pXMLDoc;


};








#endif // _XMLCONFIG_HPP_

XMLConfig.cpp :




/**//**********************************************************************************************************************


Copyright(C) 2008,soho 保留所有权利 ( All rights reserved. )




文件名称:XMLConfig.cpp


摘 要:XMLConfig类实现文件


当前版本:1.0


作 者:snail


创建日期:2008年3月30日


历 史:2008-03-30 - create


2008-03-31 - add method and test it


**********************************************************************************************************************/


#include "XMLConfig.hpp"




// Macro that calls a COM method returning HRESULT value:


#define HRCALL(a, errmsg)




do ...{


hr = (a);




if (FAILED(hr)) ...{


dprintf( "%s:%d HRCALL Failed: %s 0x%.8x = %s ",


__FILE__, __LINE__, errmsg, hr, #a );


goto clean;


}


} while (0)








/**//**********************************************************************************************************************


XMLConfig implement


**********************************************************************************************************************/






/**//******************************************************************************************


输入 : NONE


输出 : NONE


返回 : NONE


摘要 : NONE


******************************************************************************************/




XMLConfig::XMLConfig() ...{


// 初始化COM库


CoInitialize(NULL);


_pXMLDoc = NULL;


}






XMLConfig::~XMLConfig() ...{


_SafeReleaseXMLDoc();


// 卸载COM库


CoUninitialize();


}








/**//******************************************************************************************


输入 : NONE


输出 : NONE


返回 : NONE


摘要 : safe release pointer to xml file


******************************************************************************************/




void XMLConfig::_SafeReleaseXMLDoc() ...{




if(_pXMLDoc) ...{


_pXMLDoc->Release();


_pXMLDoc = NULL;


}


}






/**//******************************************************************************************


输入 : NONE


输出 : NONE


返回 : NONE


摘要 : 创建一个XML文档,成功返回true,失败返回false


******************************************************************************************/




bool XMLConfig::ConstructXMLFile() ...{


bool bRetVal = false;




// 释放以前的文档


_SafeReleaseXMLDoc();


// 创建新文档


HRESULT hr = CoCreateInstance( __uuidof(DOMDocument), NULL,


CLSCTX_INPROC_SERVER, __uuidof(IXMLDOMDocument2),


(void**)&_pXMLDoc);




if(S_OK == hr) ...{


// 创建并添加XML头


CComBSTR bstrTarget = _T("xml");


CComBSTR bstrData = _T(" version='1.0' encoding='gb2312'");


IXMLDOMProcessingInstruction* pIXMLPI = NULL;


hr = _pXMLDoc->createProcessingInstruction(bstrTarget, bstrData, &pIXMLPI);




if(S_OK == hr) ...{


hr = _pXMLDoc->appendChild(pIXMLPI, NULL);


pIXMLPI->Release();


pIXMLPI = NULL;




if(S_OK == hr) ...{


bRetVal = true;


}


}


}




// 如果失败,则删除文档,因为文档不是XML文档




if(!bRetVal) ...{


_SafeReleaseXMLDoc();


}


return bRetVal;


}






/**//******************************************************************************************


输入 : fileName


输出 : NONE


返回 : NONE


摘要 : 从文件加载一个XML文件,加载成功返回true,加载失败返回false


******************************************************************************************/




bool XMLConfig::LoadFromXMLFile(const std::string& fileName) ...{


bool bRetVal = false;




// 释放以前的文档


_SafeReleaseXMLDoc();


// 创建新文档


HRESULT hr = CoCreateInstance( __uuidof(DOMDocument), NULL,


CLSCTX_INPROC_SERVER, __uuidof(IXMLDOMDocument2),


(void**)&_pXMLDoc);




if(S_OK == hr) ...{


// 加载


VARIANT_BOOL varBool;


hr = _pXMLDoc->load(CComVariant(CComBSTR(fileName.data())), &varBool);




if((S_OK == hr) && (varBool==VARIANT_TRUE)) ...{


bRetVal = true;


}


}




// 如果失败,则删除文档,因为文档不成为一个XML文档




if(!bRetVal) ...{


_SafeReleaseXMLDoc();


}


return bRetVal;


}






/**//******************************************************************************************


输入 : fileName


输出 : NONE


返回 : NONE


摘要 : 保存XML文件到fileName,成功返回true,失败返回false


******************************************************************************************/




bool XMLConfig::SaveToXMLFile(const std::string& fileName) ...{


bool bRetVal = false;






if(_pXMLDoc) ...{


HRESULT hr = _pXMLDoc->save(CComVariant(CComBSTR(fileName.data())));




if(S_OK == hr) ...{


bRetVal = true;


}


}




return bRetVal;


}






/**//******************************************************************************************


输入 : strXML


输出 : NONE


返回 : NONE


摘要 : 获得XML文件内容


******************************************************************************************/




void XMLConfig::GetXML(std::string& strXML) ...{




if(_pXMLDoc) ...{


CComBSTR bstrXML;


USES_CONVERSION;




_pXMLDoc->get_xml(&bstrXML);


strXML = W2A(bstrXML.m_str);


}


}






/**//******************************************************************************************


输入 : nodeName - node name for new attribute


attrName - attribute name for new attribute


attrValue - attribute value for new attribute


输出 : NONE


返回 : NONE


摘要 : append new attribute at right place


******************************************************************************************/


bool XMLConfig::AppendAttributeNode(const std::string& nodeName,


const std::string& attrName,




const std::string& attrValue) ...{


bool bRetVal = false;






if(_pXMLDoc) ...{


//find doc root element, if can't find, create a new root element


IXMLDOMElement* pIRootMember = NULL;


HRESULT hr = _pXMLDoc->get_documentElement(&pIRootMember);




if(!pIRootMember) ...{ // have no root element


hr = _pXMLDoc->createElement(CComBSTR("root"), &pIRootMember);




if(S_OK == hr) ...{


IXMLDOMNode* ppOutNewChild = NULL;


hr = _pXMLDoc->appendChild(pIRootMember, &ppOutNewChild);


}


}




//find node element use 'nodeName'


CComBSTR bstrName = attrName.data();


CComBSTR bstrNDName = nodeName.data();


CComVariant value = attrValue.data();




bool bFind = false;


IXMLDOMNode* pFindND = NULL;


hr = pIRootMember->selectSingleNode(bstrNDName, &pFindND);




if(S_OK == hr) ...{ //if find, set attribute


IXMLDOMElement* pFindElement = NULL;


pFindND->QueryInterface(__uuidof(IXMLDOMElement),(void**)&pFindElement);


pFindElement->setAttribute(bstrName, value);




bFind = true;


bRetVal = true;






if(pFindElement) ...{


pFindElement->Release();


pFindElement = NULL;


}


}






if(pFindND) ...{


pFindND->Release();


pFindND = NULL;


}




//if not find, create new element and append to it




if(!bFind) ...{


IXMLDOMElement* pINewMember = NULL;


hr = _pXMLDoc->createElement(CComBSTR(nodeName.data()), &pINewMember);




if(S_OK == hr) ...{


pINewMember->setAttribute(bstrName, value);




if(S_OK == hr) ...{


IXMLDOMNode* ppOutNewChild = NULL;


hr = pIRootMember->appendChild(pINewMember, &ppOutNewChild);




if(S_OK == hr) ...{


bRetVal = true;


}


}


}






if(pINewMember) ...{


pINewMember->Release();


pINewMember = NULL;


}


}






if(pIRootMember) ...{


pIRootMember->Release();


pIRootMember = NULL;


}


}




return bRetVal;


}






/**//******************************************************************************************


输入 : nodeName - node name for new attribute


attrName - attribute name for new attribute


输出 : attrValue - attribute value for new attribute


返回 : if successful return true, else return false


摘要 : get attribute value at right place


******************************************************************************************/


bool XMLConfig::GetAttributeValue( const std::string& nodeName,


const std::string& attrName,




std::string& attrValue) ...{


bool bRetVal = false;






if(_pXMLDoc) ...{


//find doc root element, if can't find, failed get attribute


IXMLDOMElement* pIRootMember = NULL;


HRESULT hr = _pXMLDoc->get_documentElement(&pIRootMember);




if(pIRootMember) ...{


//find node element use 'nodeName'


IXMLDOMNode* pFindND = NULL;


CComBSTR bstrNDName = nodeName.data();


CComBSTR bstrAttName = attrName.data();


USES_CONVERSION;




hr = pIRootMember->selectSingleNode(bstrNDName, &pFindND);




if(S_OK == hr) ...{ //if find, get attribute


IXMLDOMElement* pFindElement = NULL;


pFindND->QueryInterface(__uuidof(IXMLDOMElement),(void**)&pFindElement);




CComVariant value;


hr = pFindElement->getAttribute(bstrAttName, &value);




if(S_OK == hr) ...{


attrValue = W2A(value.bstrVal);


bRetVal = true;


}






if(pFindElement) ...{


pFindElement->Release();


pFindElement = NULL;


}


}






if(pFindND) ...{


pFindND->Release();


pFindND = NULL;


}


}






if(pIRootMember) ...{


pIRootMember->Release();


pIRootMember = NULL;


}


}


return bRetVal;


}



main.cpp :




/**//**********************************************************************************************************************


Copyright(C) 2008,soho 保留所有权利 ( All rights reserved. )




文件名称:main.cpp


摘 要:


当前版本:1.0


作 者:snail


创建日期:2008年3月30日


历 史:


**********************************************************************************************************************/


#include "global.hpp"


#include "XMLConfig.hpp"


#include <iostream>


#include <string>


using namespace std;




template<class T>


void Println(T str)




...{


cout << str << endl;


}






/**//**********************************************************************************************************************


函数 : main


输入 : NONE


输出 : NONE


返回 : NONE


摘要 : NONE


创建日期 : 2008年3月30日


历史 : 2008年3月30日-- created.


**********************************************************************************************************************/




int main( int argc, char *argv[], char *envp[]) ...{


XMLConfig cfg;


string strXML;




if(cfg.ConstructXMLFile()) ...{


cfg.GetXML(strXML);


Println("construct xml file successful!");


Println(strXML.c_str());


}




else ...{


Println("construct xml file failed!");


}


Println("================== ");






if(cfg.AppendAttributeNode("node1", "att1", "val1")) ...{


cfg.GetXML(strXML);


Println("AppendAttributeNode xml file successful!");


Println(strXML.c_str());


}




else ...{


Println("AppendAttributeNode xml file failed!");


}


Println("================= ");






if(cfg.AppendAttributeNode("node1", "att2", "val2")) ...{


cfg.GetXML(strXML);


Println("AppendAttributeNode xml file successful!");


Println(strXML.c_str());


}




else ...{


Println("AppendAttributeNode xml file failed!");


}


Println("================= ");






if(cfg.AppendAttributeNode("node2", "att1", "val1")) ...{


cfg.GetXML(strXML);


Println("AppendAttributeNode xml file successful!");


Println(strXML.c_str());


}




else ...{


Println("AppendAttributeNode xml file failed!");


}


Println("================= ");






if(cfg.AppendAttributeNode("node2", "att2", "val2")) ...{


cfg.GetXML(strXML);


Println("AppendAttributeNode xml file successful!");


Println(strXML.c_str());


}




else ...{


Println("AppendAttributeNode xml file failed!");


}


Println("================= ");








if(cfg.AppendAttributeNode("node1", "att1", "value1")) ...{


cfg.GetXML(strXML);


Println("AppendAttributeNode xml file successful!");


Println(strXML.c_str());


}




else ...{


Println("AppendAttributeNode xml file failed!");


}


Println("================= ");






if(cfg.SaveToXMLFile("test.xml")) ...{


cfg.GetXML(strXML);


Println("save xml file to 'test.xml' successful!");


Println(strXML.c_str());


}




else ...{


Println("save xml file to 'test.xml' failed!");


}


Println("================= ");






if(cfg.LoadFromXMLFile("test.xml")) ...{


cfg.GetXML(strXML);


Println("load xml file from 'test.xml' successful!");


Println(strXML.c_str());


}




else ...{


Println("load xml file from 'test.xml' failed!");


}


Println("================= ");




string strVal;




if(cfg.GetAttributeValue("node1", "att1", strVal)) ...{


Println("GetAttributeValue successful!");


cout << "node1:att1=" << strVal.c_str() << endl;


//Println(strXML.c_str());


}




else ...{


Println("GetAttributeValue failed!");


}


Println("================= ");






if(cfg.GetAttributeValue("node1", "att2", strVal)) ...{


Println("GetAttributeValue successful!");


cout << "node1:att2=" << strVal.c_str() << endl;


//Println(strXML.c_str());


}




else ...{


Println("GetAttributeValue failed!");


}


Println("================= ");






if(cfg.GetAttributeValue("node2", "att1", strVal)) ...{


Println("GetAttributeValue successful!");


cout << "node2:att1=" << strVal.c_str() << endl;


//Println(strXML.c_str());


}




else ...{


Println("GetAttributeValue failed!");


}


Println("================= ");






if(cfg.GetAttributeValue("node2", "att2", strVal)) ...{


Println("GetAttributeValue successful!");


cout << "node2:att2=" << strVal.c_str() << endl;


//Println(strXML.c_str());


}




else ...{


Println("GetAttributeValue failed!");


}


Println("================= ");




}





null
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐