CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Miscellaneous >> Miscellaneous


XMLize Your Class
Rating: none

Dhandapani Ammasai (view profile)
March 28, 2001


(continued)



Environment: Visual C++ 6, ATL, MS XML

You may be already familiar with serializing C++ objects using MFC's CArchive class. Serialization is the process of storing the object properties and contained objects persistently, so that the object can be reconstructed to its previous state by loading the persistently stored data. If you use CArchive to serialize, the object will be serialized in to a binary form.

In this article I will show you how to serialize/deserialize a class into/from XML string.

Why Serialize a object in to a XML?

With the widespread usage and acceptance of XML as the format to exchange data over disparate systems, serializing a object in to a XML has many advantages:
  • XML documents can be easily exchanged over the web
  • Can work on disparate platforms
  • XML tree can be parsed easily by using readily available XML parsers in the market such as Microsoft XML Parser, Java XML Parser(JAXP) etc
Now, Let me show you how to do the XMLizing.

How it is done?

The generic class CXMLArchive has most of the functionality to XMLize(i.e. serialize) a class and deXMLize(i.e. deserialize) a class. Just derive your class from CXMLArchive and override the pure virtual methods to provide class specific implementation. Let's take an example and see how it works.

Example

Let's take a simple employee object and see how it can be serialized in to the schema format shown below.

Employee Schema

Uses Microsoft XDR Schema Standard

Employee Object

In employee object, do the following things to XMLize it:

In Header file

  • Include XMLArchive.h file
  • Derive employee class from CXMLArchive
  • Call XML_DECLARE_NODE_VAR_MAP(n), where n is the size of nodevarmap structure
  • Declare the following virtual functions from CXMLArchive class
    • InitXMLNodeVariableMap()
    • FillAttribute()
    • Save()

In CPP file

  • Implement the virtual methods

  • InitXMLNodeVariableMap()
    The purpose of this method is to maintain a dictionary of which XML node is related to which object variable, so that the XML node value can be correctly transferred to the appropriate object variable. Pass XML Node , data type and variable as parameters to XML_NODE_VAR_MAP macro. Note that variant data types(VT_xx) is used to specify data type.
    void CEmployee::InitXMLNodeVariableMap()
    {
    START_XML_NODE_VAR_MAP()
     //Specify xml node name, data type and the variable to hold the node value
     XML_NODE_VAR_MAP(NODE_ID, VT_I4, &m_lId)
     XML_NODE_VAR_MAP(NODE_FIRSTNAME, VT_BSTR, &m_szFirstName)
     XML_NODE_VAR_MAP(NODE_LASTNAME, VT_BSTR, &m_szLastName)
     XML_NODE_VAR_MAP(NODE_ADDRESS, VT_BSTR, &m_szAddress)
     END_XML_NODE_VAR_MAP()
    }
    
    FillAttribute()
    This method helps to initialize variables of the employee object from the XML tree. Note that all the meat is in base class and we just need call the base class helper method FillAttributeHelper() here.
    BOOL CEmployee::FillAttribute(MSXML::IXMLDOMNode* pXMLNode)
    {
     ATLASSERT(pXMLNode);
    
     return FillAttributeHelper(pXMLNode,
                                XML_NODEVARMAP_STRUCT,
                                XML_NODE_VAR_MAP_COUNT);
    }
    
    Save()
    The purpose of Save() method is build XML tree to represent employee object. Build the XML tree using helper macros like START_XML, START_XML_ELEMENT etc.
    long CEmployee::Save(MSXML::IXMLDOMNode* pCurrentNode)
    {
     //Empty DOM tree
     EmptyDOM();
    
     //Block of code below will build xml tree
     //
     XML_SET_CURRENTNODE(pCurrentNode)
     START_XML()
      START_XML_ELEMENT(NODE_EMPLOYEE)
      XML_ELEMENT_LONG(NODE_ID, m_lId)
      XML_ELEMENT_STR(NODE_FIRSTNAME, m_szFirstName)
      XML_ELEMENT_STR(NODE_LASTNAME, m_szLastName)
      XML_ELEMENT_STR(NODE_ADDRESS, m_szAddress)
      END_XML_ELEMENT()
     END_XML()
    
     return S_OK;
    }
    

  • XMLize() method
  • XMLize() and deXMLize() are exposed methods in employee object to serialize and deserialize. In XMLize() method, call Save() method to build XML Tree. Once the XML tree is build, it is easy to get the XML string by calling IXMLDOMDocument::get_xml() method.

  • deXMLize() method
  • In deXMLize() method, Just call the base class method LoadXML(), which takes care of exploring each XML node and transferring the values from XML node to appropriate object variable.

Contained Objects

So far so good. Will this framework work if employee object contains any child objects?. The answer is a resounding yes. This framework supports contained objects XMLization too. You just have to implement ReadContainedObject() in your derived class(employee in this case). In overridden ReadContainedObject() method, just check if the node name is of the contained object and if yes call the contained object LoadXMLNode() method and set bContainedObjectFound to TRUE.

That's it. Happy XMLization.

Downloads

Download source - 35 Kb
Download demo project - 39 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Updated Code?? - Legacy CodeGuru (03/30/2001)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES