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;}
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several