devxlogo

Reading Configuration Information from an XML File

Reading Configuration Information from an XML File

This tip helps to read configuration information in an XML file. Using XPath, the values can be read simply and the value is fetched from the file as CString.

/***********  The header file ******************************/class CXMLConfig {private:	IXMLDOMDocument* m_pXMLDocument;	CString m_strFileName;public:	CXMLConfig(void);	~CXMLConfig(void);	CXMLConfig(CString strConfigFileName);	HRESULT LoadXMLDocument();	CString GetValue(CString strXPath);	void SetFileName(CString strFileName);};/***********  The implementation file ******************************/#include "stdafx.h"#include ".xmlconfig.h"CXMLConfig::CXMLConfig(void){	CoInitialize(NULL);	m_pXMLDocument = NULL;	m_strFileName = "";}CXMLConfig::~CXMLConfig(void){	if (m_pXMLDocument!=NULL) {		m_pXMLDocument->Release();		m_pXMLDocument = NULL;	}	CoUninitialize();}CXMLConfig::CXMLConfig(CString strConfigFileName){	m_strFileName = strConfigFileName;}void CXMLConfig::SetFileName(CString strFileName){	m_strFileName = strFileName;}BOOL CXMLConfig::LoadXMLDocument(BOOL bValidateOnParse , BOOL bResolveExternals ){	HRESULT hRes ;	BOOL bRet = FALSE;	CFileFind ff;	if (!ff.FindFile(m_strFileName)) {		CString strMsg;		strMsg.Format("The file '%d' is not found",m_strFileName);		AfxMessageBox(strMsg);		return bRet;	}   	hRes = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,IID_IXMLDOMDocument, (void**)&m_pXMLDocument);	if ( hRes == S_OK ) {		COleVariant var(m_strFileName) ;		VARIANT_BOOL bResult;		hRes = m_pXMLDocument->load(var,&bResult) ;		if ( hRes == S_OK && bResult == VARIANT_TRUE ) {			//	Success			bRet = TRUE;		}		else {			AfxMessageBox("file can not be loaded");		}	}	return bRet ;}/********************************************************************************Function		:	GetValueParameters		:	strXPath	:	xPath to query from the xml file*********************************************************************************/CString CXMLConfig::GetValue(CString strXPath){	CString strValue = "";	CComPtr pRoot;	CComPtr pXMLDOMNodeResult;	CComPtr pElement;	CComPtr pText;	CComPtr pXMLDOMNode;	BSTR bStrXPath;	m_pXMLDocument->get_documentElement(&pRoot);	if ( pRoot == NULL ) {		return strValue;	}	bStrXPath = strXPath.AllocSysString() ;	pRoot->selectSingleNode(bStrXPath,&pXMLDOMNodeResult);	::SysFreeString(bStrXPath) ;	if ( pXMLDOMNodeResult == NULL ) {		return strValue;	}	pXMLDOMNodeResult->QueryInterface(IID_IXMLDOMElement,(void**)&pElement);	if ( pElement == NULL ) {		return strValue;	}	pElement->get_firstChild(&pXMLDOMNode);	if ( pXMLDOMNode == NULL ) {		return strValue;	}	pXMLDOMNode->QueryInterface(IID_IXMLDOMText,(void**)&pText);	if ( pText == NULL ) {		return strValue;	}	BSTR bStrTextContent ;	pText->get_text(&bStrTextContent) ;	strValue = CString(bStrTextContent) ;	return strValue;}
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist