//*****************************************************************************
// Module : XmloParser.cpp
//
// Copyright 2004 by ITC, ED4-4. All rights reserved.
//
// Author: Liu, Miao
//
// Date: 2005/02/03
//
// Description: Process XML File Functions
//
// Platforms: Windows2K,XP
//
// Functions:
//
// Modified history:
//*****************************************************************************
#include <stdio.h>
#include <objbase.h>
#include "XmlParser.h"
//*****************************************************************************
// Function: CProXmlFile(const char * fileName)
//
// Purpose: Constructor function
//
// Parameters: fileName->name of the file which will be handled
//
// Return value: none
//
// Modified history:
//*****************************************************************************
CProXmlFile::CProXmlFile(IN const char * fileName)
{
strcpy(m_pszXmlFile, fileName);
HANDLE hFile;
hFile = CreateFile(m_pszXmlFile, // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
m_bXmlFileExist = FALSE;
}
else
{
m_bXmlFileExist = TRUE;
}
CloseHandle(hFile);
CoInitialize(NULL); // Register with COM
if (!m_bXmlFileExist)
{
CreateXmlFile(m_pszXmlFile);
}
SetXMLFileName(m_pszXmlFile);
}
//*****************************************************************************
// Function: ~CProXmlFile()
//
// Purpose: DisConstructor function
//
// Parameters: none
//
// Return value: none
//
// Modified history:
//*****************************************************************************
CProXmlFile::~CProXmlFile()
{
if (NULL != m_pDoc)
{
m_pDoc = NULL;
//m_pDoc->Release();
}
CoUninitialize(); // Unregister with COM
}
//******************************************************************************************************************
// Function: SetXMLFileName(IN const char * fileName)
//
// Purpose: Set the XML file name which will be handled
//
// Parameters: fileName->the XML file name which will be handled
//
// Return value: If set the fileName successfully, return TRUE, or return FALSE
//
// Modified history:
//******************************************************************************************************************
BOOL CProXmlFile::SetXMLFileName(IN const char * fileName)
{
HRESULT hr = m_pDoc.CreateInstance(__uuidof(xml::DOMDocument));
if( !SUCCEEDED(hr) )
{
return FALSE;
}
//Load the xml file
m_pDoc->load(m_pszXmlFile);
return TRUE;
}
//******************************************************************************************************************
// Function: GetXmlString(IN const char* pszSection, IN const char* pszKey, OUT char*pszValue)
//
// Purpose: Get String Value according to parent node name and child node name
//
// Parameters:pszSection->the section name
// pszKey->the key name
// pszValue->value to be filled
//
// Return value: If get it, return TRUE, or return FALSE
//
// Modified history:
//******************************************************************************************************************
BOOL CProXmlFile::GetXmlString(IN const char* pszSection, IN const char* pszKey, OUT char*pszValue)
{
xml::IXMLDOMNodePtr pElement = NULL;
char szPath[512];
strcpy(pszValue, "");
//Find the key
sprintf(szPath, "//%s//%s//%s", ROOT_NODE, pszSection, pszKey);
pElement = (xml::IXMLDOMElementPtr)(m_pDoc->selectNodes((_bstr_t)szPath));
//pElement = (xml::IXMLDOMElementPtr)(m_pDoc->selectSingleNode((_bstr_t)szPath));
if ( NULL == pElement )
{
return FALSE;
}
//Get the key's value
BSTR var;
HRESULT hr = pElement->get_text(&var);
if ( !SUCCEEDED(hr) )
{
return FALSE;
}
strcpy( pszValue, (char*)(_bstr_t)var );
return TRUE;
}
//******************************************************************************************************************
// Function: CreateXmlFile(IN const char* pszWholeName)
//
// Purpose: Create the XML File: config.xml
//
// Parameters: pszWholeName->path and name of the XML file
//
// Return value: If create sussesfully, return TRUE, or return FALSE
//
// Modified history:
//******************************************************************************************************************
BOOL CProXmlFile::CreateXmlFile(IN const char* pszWholeName)
{
xml::IXMLDOMDocumentPtr pDoc = NULL;
xml::IXMLDOMElementPtr pXmlRoot = NULL;
xml::IXMLDOMProcessingInstructionPtr spXMLPI;
HRESULT hr = pDoc.CreateInstance(__uuidof(xml::DOMDocument));
if( !SUCCEEDED(hr) )
{
return FALSE;
}
spXMLPI = pDoc->createProcessingInstruction("xml", "version=\"1.0\"");
pDoc->raw_createElement((_bstr_t)ROOT_NODE, &pXmlRoot);
pDoc->raw_appendChild(pXmlRoot, NULL);
pDoc->save(pszWholeName);
pDoc.Release();
return TRUE;
}
//******************************************************************************************************************
// Function: WriteXmlString(IN const char* pszSection, IN const char* pszKey, IN char*pszValue)
//
// Purpose: Set String Value according to section name and key name
//
// Parameters: pszSection->the section name
// pszKey->the key name
// pszValue->value to be set
//
// Return value: If set it, return TRUE, or return FALSE
//
// Modified history:
//******************************************************************************************************************
BOOL CProXmlFile::WriteXmlString(IN const char* pszSection, IN const char* pszKey, IN char*pszValue)
{
xml::IXMLDOMNodePtr pNode;
xml::IXMLDOMNodePtr pElement;
char szPath[512];
//Find the key
sprintf(szPath, "//%s//%s//%s", ROOT_NODE, pszSection, pszKey);
pElement = (xml::IXMLDOMElementPtr)( m_pDoc->selectSingleNode((_bstr_t)szPath) );
if ( NULL != pElement )//Find the key
{
if( !DeleteKey(pszSection, pszKey) )
{
return FALSE;
}
}
else //Cannot find the key
{
if ( !FindSection(pszSection) )
{
if (!AddSection(pszSection))
{
return FALSE;
}
}
}
//Find the section
sprintf(szPath, "//%s//%s", ROOT_NODE, pszSection);
pNode = (xml::IXMLDOMElementPtr)( m_pDoc->selectSingleNode((_bstr_t)szPath) );
AddArticleInfo(pNode, pszKey);
//Find the key again
sprintf(szPath, "//%s//%s//%s", ROOT_NODE, pszSection, pszKey);
pElement = pNode->ownerDocument->selectSingleNode((_bstr_t)szPath);
AddText(pElement, pszValue);
m_pDoc->save(m_pszXmlFile);
return TRUE;
}
//******************************************************************************************************************
// Function: DeleteSection(IN const char* pszSection)
//
// Purpose: Delete a section
//
// Parameters: pszSection->the section name
//
// Return value: If delete it, return TRUE, or return FALSE
//
// Modified history:
//******************************************************************************************************************
BOOL CProXmlFile::DeleteSection(IN const char* pszSection)
{
xml::IXMLDOMNodePtr pNode;
xml::IXMLDOMNodePtr pRoot;
char szPath[512];
//Find the section
sprintf(szPath, "//%s//%s",ROOT_NODE,pszSection);
pNode = (xml::IXMLDOMElementPtr)(m_pDoc->selectSingleNode(szPath));
if (NULL != pNode)
{
//Now delete the section
sprintf(szPath, "//%s", ROOT_NODE);
pRoot = (xml::IXMLDOMElementPtr)(m_pDoc->selectSingleNode(szPath));
HRESULT hr = pRoot->removeChild(