advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Expertise: Beginner
Language: C++
November 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;
}

It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com.
Already a member?





Shaik Mydeen
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
advertisement
advertisement