Click here to Skip to main content
Licence 
First Posted 19 Jul 2003
Views 98,965
Downloads 1,374
Bookmarked 39 times

XMLManager - XML serialization class

By Alex Hazanov | 19 Jul 2003
XMLManager class - wraps essential XML functions allowing easy access and managment of XML data.
1 vote, 9.1%
1
1 vote, 9.1%
2
4 votes, 36.4%
3
2 votes, 18.2%
4
3 votes, 27.3%
5
3.81/5 - 11 votes
1 removed
μ 3.64, σa 2.26 [?]

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

About the Author

Alex Hazanov

Software Developer (Senior)
RDV Systems
Israel Israel

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionProblem in using the class PinmemberDaisydale20:33 28 May '09  
Generaldon't compiles PinmemberMember 19357209:09 9 Oct '08  
GeneralExample of usage. Pinmemberrohinivn1:47 14 Mar '07  
QuestionHow do i add msxml.dll to my installer ? Pinmembercode4jigar3:13 5 Sep '06  
GeneralNew class for XML management! PinmemberAlex Hazanov2:15 27 May '04  
GeneralSmall errors in usage code... PinmemberFrançois Gasnier2:56 26 Feb '04  
GeneralRe: Small errors in usage code... Pinmemberandré_k21:33 7 Dec '05  
GeneralGreat Class PinmemberBBWoof19:35 22 Jan '04  
GeneralRe: Great Class PinmemberAlex Hazanov20:22 24 Jan '04  
GeneralProblem using class Pinmembergenehsd20:40 23 Dec '03  
GeneralRe: Problem using class PinmemberAlex Hazanov22:17 23 Dec '03  
GeneralRe: Problem using class Pinmembergenehsd5:15 24 Dec '03  
GeneralRe: Problem using class PinmemberAlex Hazanov6:52 24 Dec '03  
GeneralRe: Problem using class Pinmembergenehsd7:15 24 Dec '03  
GeneralRe: Problem using class PinmemberAlex Hazanov11:33 24 Dec '03  
GeneralRe: Problem using class PinsussRichard O.11:30 3 Feb '04  
GeneralRe: Problem using class PinmemberPaul Perkins5:24 12 Mar '04  
GeneralRe: Problem using class Pinmemberpixelgrease16:17 24 Mar '04  
GeneralRe: Problem using class Pinmemberdarn_deng23:21 26 May '04  
GeneralRe: Problem using class PinmemberAlex Hazanov2:11 27 May '04  
GeneralProblem PinmemberHoward.Hsu23:18 3 Dec '03  
GeneralRe: Problem PinmemberAlex Hazanov22:20 6 Dec '03  
GeneralStyle issue Pinmemberdog_spawn1:27 20 Jul '03  
GeneralRe: Style issue PinmemberAlex Hazanov1:35 20 Jul '03  
GeneralRe: Style issue Pinmemberdog_spawn1:39 20 Jul '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 20 Jul 2003
Article Copyright 2003 by Alex Hazanov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid