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
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds