XMLManager - XML serialization class






3.88/5 (11 votes)
Jul 20, 2003

141387

1914
XMLManager class - wraps essential XML functions allowing easy access and managment of XML data.
Introduction
This wrapper class - wraps around functions for XML access, storage and retrieval of data. It allows easy access and manipulation of data stored as an XML file. The usage is very easy and is perfect for any serialization needs especially of a tree based data.
Usage
First include the XMLManager.h and XMLManager.cpp files in your project.
Create an instance of the class:
CXMLManager xmlManager;
Don't forget to include the XML support in your project:
#import "msxml.dll" named_guids raw_interfaces_only
Usually in your stdafx.h.
Create an XML document pointer:
HRESULT hr = S_OK;
MSXML::IXMLDOMDocument *pDoc = NULL;
CoInitialize(NULL);
CHECKHR(CoCreateInstance(MSXML::CLSID_DOMDocument, NULL,
CLSCTX_INPROC_SERVER,
MSXML::IID_IXMLDOMDocument,
(void**)&pDoc));
Now use the XMLManager in the folowing way:
Serialization:
// Load the XML file HRESULT CXMLManager::LoadDocument( MSXML::IXMLDOMDocument *pDoc, // Pointer to XML Document you created char* fileName // The file name (C:\mydata.xml) ); // Save the document to a file HRESULT CXMLManager::SaveDocument( MSXML::IXMLDOMDocument *pDoc, // Pointer to XML Document char* fileName // Desired filename (rewrite if exists) );
Data retrieval:
// Get child node of any parent node MSXML::IXMLDOMNode* CXMLManager::GetChild( MSXML::IXMLDOMNode *pNode, // The parent node CString name // The name of the desired child ); // Retrieve the name of XML node CString CXMLManager::GetNodeName( MSXML::IXMLDOMNode *pNode // The node who's name is required ); // Three functions to get attribute data from node long CXMLManager::GetIntegerAttribute( // The node from which to extract the attribute MSXML::IXMLDOMNode* pNode, CString attName // Attribute name ); double CXMLManager::GetDoubleAttribute( MSXML::IXMLDOMNode* pNode, CString attName ); CString CXMLManager::GetStringAttribute( MSXML::IXMLDOMNode* pNode, CString attName );
Data storage:
// Create XML node MSXML::IXMLDOMNode * CXMLManager::CreateDOMNode( // The XML document where to create the node MSXML::IXMLDOMDocument* pDoc, int type, // Node type CString nodeName // Node name );
// Three functions to strore data in sttributes CXMLManager::SetIntegerAttribute( MSXML::IXMLDOMNode* pNode, // Pointer to the node where to store data CString attName, // Attribute name long val // The value ); CXMLManager::SetDoubleAttribute( SXML::IXMLDOMNode* pNode, String attName, double val ); CXMLManager::SetStringAttribute( MSXML::IXMLDOMNode* pNode, CString attName, CString val );
Example of usage
MSXML::IXMLDOMDocument *pDoc = NULL; CoInitialize(NULL); // Create the document CHECKHR(CoCreateInstance(MSXML::CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER, MSXML::IID_IXMLDOMDocument, (void**)&pDoc)); // Load the document contents from file CHECKHR(xmlManager.LoadDocument(pDoc,"C:\\Test.xml")); // Create new node MSXML::IXMLDOMNode *pNode = pNode = xmlManager.CreateDOMNode( pDoc,MSXML::NODE_ELEMENT, "New Node"); // Add string to the node xmlManager.SetStringAttribute(pNode,"String","Sample data"); // Add integer to the node xmlManager.SetStringAttribute(pNode,"Integer",56872); // Add double to the node xmlManager.SetStringAttribute(pNode,"Double",35.264); // Append the new node to the document pDoc->appendChild(pNode, NULL); // Save the updated XML document to file xmlManager.SaveDocument(pDoc,,"C:\\Test.xml"));