Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Nov 3, 2004

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		:	GetValue
Parameters		:
	strXPath	:	xPath to query from the xml file
*********************************************************************************/
CString CXMLConfig::GetValue(CString strXPath)
{
	CString strValue = "";
	CComPtr<IXMLDOMElement> pRoot;
	CComPtr<IXMLDOMNode> pXMLDOMNodeResult;
	CComPtr<IXMLDOMElement> pElement;
	CComPtr<IXMLDOMText> pText;
	CComPtr<IXMLDOMNode> 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;
}
Shaik Mydeen
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap