A Simple DOM-Based XML Manager Class

Introduction

CXMLMgr is a simple XML manager class that can be used to manipulate XML files. It uses the MSXML2 namespace. You should have MSXML 4.0 SDK to use it.

Details

The interface of the class is as follows:

CXMLMgr::CXMLMgr()
Description It will initlize COM and set the default memory handler
 
CXMLMgr::~CXMLMgr()
Description           Destory & Release resources
 
HRESULT CXMLMgr::Initlize() const
Parameters None
Return HERSULT
   S_OK (on success)
   E_FAIL (on error)
Description It will initlize Document pointer
 
bool CXMLMgr::Load(LPCSTR lpcstrXMLFileName)
Parameters [in] LPCSTR
   Name & path of the .xml file to load
Return bool
   true (on success)
   false (on error)
Description It will load a valid XML document
 
HRESULT CXMLMgr::Save(LPCSTR lpcstrXMLFileName)
Parameters [in] LPCSTR
   Name & path with which the .xml file is required to be stored
Return HRESULT
   S_OK (on success)
   E_FAIL (on error)
Description Save the document to the external .xml file
 
void CXMLMgr::Release()
Parameters None
Return None
Description Release the resources
 
void CXMLMgr::Release(MSXML2::IXMLDOMDocument* pXMLDoc)
Parameters [in] MSXML2::IXMLDOMDocument*
   Document pointer
Return None
Description Release document pointer
 
void CXMLMgr::Release(MSXML2::IXMLDOMElement* pXMLElement)
Parameters [in] MSXML2::IXMLDOMElement*
   Element to release
Return None
Description Release element pointer
 
LPCSTR CXMLMgr::GetText(MSXML2::IXMLDOMNode* pElement) const
Parameters [in] MSXML2::IXMLDOMNode*
   Element which text is required
Return LPCSTR
   Text of the XML Element
Description Get the text of the node
 
LPCSTR CXMLMgr::GetNodeName(MSXML2::IXMLDOMNode* pNode) const
Parameters [in] MSXML2::IXMLDOMNode*
   Node, which name is required
Return LPCSTR
   Name of the node
Description Get the name of the node
 
LPCSTR CXMLMgr::ReturnBuffer(BSTR bstr) const
Parameters [in] BSTR
   BSTR string, which is required to be in LPCSTR
Return LPCSTR
   Output of BSTR
Description Convert BSTR string to LPCSTR and return it
 
MSXML2::IXMLDOMNode* CXMLMgr::GetFirstChild(MSXML2::IXMLDOMNode* pNode) const
Parameters [in] MSXML2::IXMLDOMNode*
   Node, which first child is required
Return MSXML2::IXMLDOMNode*
   First child of the node
Description Get the first element of the node
 
MSXML2::IXMLDOMNode* CXMLMgr::GetFirstChild() const
Parameters None
Return MSXML2::IXMLDOMNode*
   First child of the root element
Description Get the first element of the root node
 
MSXML2::IXMLDOMNode * CXMLMgr::GetLastChild(MSXML2::IXMLDOMNode * pNode) const
Parameters [in] MSXML2::IXMLDOMNode*
   Node, which last element is required
Return MSXML2::IXMLDOMNode*
   Last element of the node
Description Get the last element of the node
 
MSXML2::IXMLDOMNode * CXMLMgr::GetLastChild() const
Parameters None
Return MSXML2::IXMLDOMNode*
   Last child of the root
Description Get the last element of the root node
 
MSXML2::IXMLDOMNode * CXMLMgr::GetNextSibling(MSXML2::IXMLDOMNode* pNode) const
Parameters [in] MSXML2::IXMLDOMNode*
   Node, which sibling is required
Return MSXML2::IXMLDOMNode*
   Sibling node pointer
Description Get the next sibling of the node
 
MSXML2::IXMLDOMNodeList * CXMLMgr::GetChildList(MSXML2::IXMLDOMNode* pNode) const
Parameters [in] MSXML2::IXMLDOMNode*
   Node, which child list is required
Return MSXML2::IXMLDOMNodeList*
   Child list
Description Get the list of the child nodes contained by the node
 
const long CXMLMgr::GetNoOfChilds(MSXML2::IXMLDOMNodeList* pList) const
Parameters [in] MSXML2::IXMLDOMNodeList*
   Child node list pointer
Return long
   Number of children
Description Return the number of children of the node list
 
MSXML2::IXMLDOMNode * CXMLMgr::GetItemNode(const long lIndex, MSXML2::IXMLDOMNodeList* pList) const
Parameters [in] const long lIndex
   Index of the node in the list
[in] MSXML2::IXMLDOMNodeList*
   Pointer of the node list
Return MSXML2::IXMLDOMNode*
   Node item in the list
Description Return node, which index and child list is provided
 
LPCSTR CXMLMgr::GetItemText(MSXML2::IXMLDOMNode* pNode) const
Parameters [in] MSXML2::IXMLDOMNode*
   Node, which text is required
Return LPCSTR
   Text of the node
Description Get the text of the node
 
HRESULT CXMLMgr::CreateRoot(LPCSTR lpcstrRootName, LPCSTR lpcstrRootText)
Parameters [in] LPCSTR lpcstrRootName
   Name with which root element is required to be created
[in] LPCSTR lpcstrRootText
   Text of the root element (can be NULL)
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Create Root element with root text. Note: If there is no text required for the root element, it can set to NULL.
 
HRESULT CXMLMgr::AppendChildToParent(MSXML2::IXMLDOMNode *pChild, MSXML2::IXMLDOMNode *pParent)
Parameters [in] MSXML2::IXMLDOMNode* pChild
   Child node to append
[in] MSXML2::IXMLDOMNode* pParent
   Parent node, to which child will be appended
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Append one node to another
 
HRESULT CXMLMgr::InsertChild(MSXML2::IXMLDOMNode *pNewNode, MSXML2::IXMLDOMNode *pRefNode, MSXML2::IXMLDOMNode *pParentNode)
Parameters [in] MSXML2::IXMLDOMNode *pNewNode
   New node to insert
[in] MSXML2::IXMLDOMNode *pRefNode
   The address of the reference node; the newChild parameter is inserted to the left of the refChild parameter. If Null, the newChild parameter is inserted at the end of the child list.
[in] MSXML2::IXMLDOMNode *pParentNode
   Parent node, to which new child is required to be added
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Insert new child
 
HRESULT CXMLMgr::CreateXMLFileHeader()
Parameters None
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Create XML Header
 
HRESULT CXMLMgr::CreateComments(LPCSTR lpcstrComment)
Parameters [in] LPCSTR
   Comments
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Write particular comments to document
 
HRESULT CXMLMgr::CreateElement(LPCSTR lpcstrElementName, LPCSTR lpcstrElementText, MSXML2::IXMLDOMNode* pParentNode, bool bAdd, int nLevel)
Parameters [in] LPCSTR lpcstrElementName
   Name of the element to be created
[in] LPCSTR lpcstrElementText
   Text of the element to be created (Can be NULL)
[in] MSXML2::IXMLDOMNode*
   Parent Node, to which the new element is required to be added
[in] bool
   true (to immediately add the element to the parent)
   false (just create the element, but don’t add now)
[in] int
   Indentation level
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Create a new element and add to the parent node (if required)
 
void CXMLMgr::AddWhiteSpaceToNode(MSXML2::IXMLDOMDocument* pDOMDoc, BSTR bstrWs, MSXML2::IXMLDOMNode* pNode)
Parameters [in] MSXML2::IXMLDOMDocument* pDOMDoc
   Document pointer
[in] BSTR bstrWs
   White space BSTR
[in] MSXML2::IXMLDOMNode* pNode
   Node to which white space is required to be created
Return None
Description Create white spaces
 
MSXML2::IXMLDOMElement* CXMLMgr::CreateNode(LPCSTR lpcstrRootName, MSXML2::IXMLDOMElement* pNode )
Parameters [in] LPCSTR
   A string specifying the name for the new element node
[in] MSXML2::IXMLDOMElement*
   The IXMLDOMElement interface for the new element
Return MSXML2::IXMLDOMElement*
   New element created
Description Creates an element node using the specified name
 
HRESULT CXMLMgr::SetNodeText(LPCSTR lpcstrText, MSXML2::IXMLDOMElement* pNode)
Parameters [in] LPCSTR
   Node text to set
[in] MSXML2::IXMLDOMElement*
   Node, which text is required to be set
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Set the text of the specified node
 
void CXMLMgr::SetIndentLevel(int nLevel, MSXML2::IXMLDOMNode* pParentNode)
Parameters [in] int
   Indend level
[in] MSXML2::IXMLDOMNode*
   Node, which indent level is required to be set
Return None
 
HRESULT CXMLMgr::ReplaceElement(MSXML2::IXMLDOMElement* pNewElement, MSXML2::IXMLDOMElement* pOldElement, MSXML2::IXMLDOMNode* pParent)
Parameters [in] MSXML2::IXMLDOMElement* pNewElement
   New element
[in] MSXML2::IXMLDOMElement* pOldElement
   Element to be replaced
[in] MSXML2::IXMLDOMNode* pParent
   Parent which contains the element that is required to be replaced
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Replace an existing element with the new element.
 
HRESULT CXMLMgr::RemoveElement(MSXML2::IXMLDOMElement* pElementToRemove, MSXML2::IXMLDOMNode* pParent)
Parameters [in] MSXML2::IXMLDOMElement* pElementToRemove
   Element to be removed
[in] MSXML2::IXMLDOMNode* pParent
   Parent that contains the element, required to be removed
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Remove specified element from the parent node
 
HRESULT CXMLMgr::RemoveElement(MSXML2::IXMLDOMElement* pElementToRemove)
Parameters [in] MSXML2::IXMLDOMElement*
   Element to be removed. It will determine its parent itself
Return HRESULT
   S_OK (on Success)
   E_FAIL (on Error)
Description Remove a specified element
 
MSXML2::IXMLDOMNode* CXMLMgr::GetParent(MSXML2::IXMLDOMElement* pElement) const
Parameters [in] MSXML2::IXMLDOMElement*
   Element to which parent is required
Return MSXML2::IXMLDOMNode*
   Parent of the element
Description Return the parent of the specified element
 
MSXML2::IXMLDOMNode* CXMLMgr::GetChild(const long lIndex, MSXML2::IXMLDOMNodeList* pList) const
Parameters [in] const long lIndex
   Index of the child to get
[in] MSXML2::IXMLDOMNodeList*
   Parent list, that contains the child
Return MSXML2::IXMLDOMNode*
   child pointer
Description Get the child element from a parent list, which index is specified.
 
void CXMLMgr::Destroy()
Parameters None
Return None
Description Release resources

More by Author

Get the Free Newsletter!

Subscribe to Data Insider for top news, trends & analysis

Must Read