Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC
Article

XMLManager - XML serialization class

Rate me:
Please Sign up or sign in to vote.
3.88/5 (12 votes)
19 Jul 2003 139.6K   1.9K   41   30
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"));

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) RDV Systems
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProblem in using the class Pin
Daisydale28-May-09 19:33
Daisydale28-May-09 19:33 
Generaldon't compiles Pin
Member 19357209-Oct-08 8:09
Member 19357209-Oct-08 8:09 
GeneralExample of usage. Pin
rohinivn14-Mar-07 0:47
rohinivn14-Mar-07 0:47 
QuestionHow do i add msxml.dll to my installer ? Pin
code4jigar5-Sep-06 2:13
code4jigar5-Sep-06 2:13 
GeneralNew class for XML management! Pin
Alex Hazanov27-May-04 1:15
Alex Hazanov27-May-04 1:15 
GeneralSmall errors in usage code... Pin
François Gasnier26-Feb-04 1:56
François Gasnier26-Feb-04 1:56 
GeneralRe: Small errors in usage code... Pin
andré_k7-Dec-05 20:33
andré_k7-Dec-05 20:33 
GeneralGreat Class Pin
BBWoof22-Jan-04 18:35
BBWoof22-Jan-04 18:35 
GeneralRe: Great Class Pin
Alex Hazanov24-Jan-04 19:22
Alex Hazanov24-Jan-04 19:22 
GeneralProblem using class Pin
genehsd23-Dec-03 19:40
genehsd23-Dec-03 19:40 
GeneralRe: Problem using class Pin
Alex Hazanov23-Dec-03 21:17
Alex Hazanov23-Dec-03 21:17 
GeneralRe: Problem using class Pin
genehsd24-Dec-03 4:15
genehsd24-Dec-03 4:15 
GeneralRe: Problem using class Pin
Alex Hazanov24-Dec-03 5:52
Alex Hazanov24-Dec-03 5:52 
GeneralRe: Problem using class Pin
genehsd24-Dec-03 6:15
genehsd24-Dec-03 6:15 
GeneralRe: Problem using class Pin
Alex Hazanov24-Dec-03 10:33
Alex Hazanov24-Dec-03 10:33 
GeneralRe: Problem using class Pin
Richard O.3-Feb-04 10:30
Richard O.3-Feb-04 10:30 
GeneralRe: Problem using class Pin
Paul Perkins12-Mar-04 4:24
Paul Perkins12-Mar-04 4:24 
GeneralRe: Problem using class Pin
pixelgrease24-Mar-04 15:17
pixelgrease24-Mar-04 15:17 
GeneralRe: Problem using class Pin
darn_deng26-May-04 22:21
darn_deng26-May-04 22:21 
GeneralRe: Problem using class Pin
Alex Hazanov27-May-04 1:11
Alex Hazanov27-May-04 1:11 
GeneralProblem Pin
Howard.Hsu3-Dec-03 22:18
Howard.Hsu3-Dec-03 22:18 
GeneralRe: Problem Pin
Alex Hazanov6-Dec-03 21:20
Alex Hazanov6-Dec-03 21:20 
GeneralStyle issue Pin
dog_spawn20-Jul-03 0:27
dog_spawn20-Jul-03 0:27 
CString CXMLManager::GetStringAttribute(MSXML::IXMLDOMNode* pNode, CString attName);

I think this is cumbersome. Do you agree that something like this is better:

string XmlElement::getStringAttribute(string name);

I always want the object I am using to have the relevant functions, rather than having to use another object to get the information indirectly Confused | :confused:

dog_spawn
http://hatekill.yojutsu.com[^]
GeneralRe: Style issue Pin
Alex Hazanov20-Jul-03 0:35
Alex Hazanov20-Jul-03 0:35 
GeneralRe: Style issue Pin
dog_spawn20-Jul-03 0:39
dog_spawn20-Jul-03 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.