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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may