The Wayback Machine - https://web.archive.org/web/20140713033236/http://www.codeguru.com/cpp/data/data-misc/xml/article.php/c15437/Code-XML-Serializer.htm

Code: XML Serializer

What the Code Does

This is a set of macros and a class for serializing and deserializing XML files and XML buffer in C++. The serializer uses an ATL String and ATL collections. It provides object representation of XML files and means to store and load objects to and from XML. This is something similar to the XmlSerializer option in C#, but becausee there is no reflection option with C++, one has to write the classes representing XML. This code is developed in VS 2005.

Simple Example

Start with a simple XML code.

Step 1: Creating a class representation

Create a class representation of the XML as follows:

class CEmployee
{
public:
   CAtlString m_csName;
   DWORD      m_dwAge;

};

Step 2: Making the above class serializable

Make the above class serializable, as shown here:

class CEmployee
{
public:
   CAtlString m_csName;
   DWORD      m_dwAge;

   DECLARE_XML_MAP("employee")
};

BEGIN_XML_MAP(CEmployee)
   XML_STR_ATTRIBUTE("name",m_csName)
   XML_DWORD_OBJECT ("age", m_dwAge)
END_XML_MAP()

Step 3: Loading and storing XML

Load and store the XML in this manner:

CEmployee oEmployee;
oEmployee.m_csName= L"xyz";
oEmployee.m_dwAge = 25;

CXmlSerializer<CEmployee>::Serialize( L"c:\\emp.xml", oEmployee);

CEmployee oLoadedEmployee;
CXmlSerializer<CEmployee>::Deserialize(
L"c:\\emp.xml", oLoadedEmployee):

Macros Defined

Macro Definition
DECLARE_XML_MAP(name) To be within class definition. name is the node name.
BEGIN_XML_MAP(xclass) xclass is name of the class.
END_XML_MAP() BEGIN_XML_MAP has to end with END_XML_MAP().
XML_CDATA_VALUE(variable) Used to read/write CDATA value in a node.
XML_CDATA_OBJECT(name, variable) Used to read/write CDATA node.
XML_STR_ATTRIBUTE(name,variable) Toread/write a string attribute value in a node.
XML_INT64_ATTRIBUTE(name,variable) To read/write anf __int64 attribute value in a node.
XML_INT_ATTRIBUTE(name,variable)/tt> To read/write an int attribute value in a node.
XML_DWORD_ATTRIBUTE(name,variable) To read/write a DWORD attribute value in a node.
XML_ARRAY_ATTRIBUTES(variable) To read/write the attributes of a node that are not pre-defined. Here, the variable has to be of type CAtlMap<CAtlString,CAtlString>.
XML_ARRAY(xclass,xarray) To read/write a node list. xclass is name of the node and xarray is of type CAtlArray<xclass*>. Note that each item in the array has to be cleared (deleted) by the user.
XML_STR_OBJECT(name,variable) To read/write the node value as a string.
XML_INT_OBJECT(name,variable) To read/write the node value as an int.
XML_INT64_OBJECT(name,variable) To read/write the node value as __int64.
XML_DWORD_OBJECT(name,variable) To read/write the node value as a DWORD.
XML_OBJECT_EX(xclass,variable) To read/write the embedded objects within an object.


Downloads

Comments

  • jTsInw wf rp OHY ghsh RW

    Posted by SUJNbNeozG on 02/04/2013 05:31pm

    buy soma soma muscle relaxer while pregnant - buy soma muscle relaxant online

    Reply

Top White Papers and Webcasts

  • Live Event Date: July 22, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In this WhatWorks analysis, John Pescatore examines a use case where end users had local administrative rights on their PCs and it had gotten out of hand for this Fortune 500 Energy and Utilities company. The compelling event that prompted the company to reexamine this situation was the migration to Windows 7. In Windows XP, a custom tool that allowed users one of three levels of administrative rights to their workstations would need to be …

  • As a result of corporate expansions, mergers, and acquisitions, the teams and technologies that support an organization's IT service management (ITSM) practices can over time become somewhat dispersed. Supporting an organization's strategic objectives, and providing consistent and quality IT support is essential, but this can be challenging where disconnected support environments exist. While physically centralizing support is not always possible, nor is it always desirable, delivering a consistent and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds