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
   CAtlString m_csName;
   DWORD      m_dwAge;


Step 2: Making the above class serializable

Make the above class serializable, as shown here:

class CEmployee
   CAtlString m_csName;
   DWORD      m_dwAge;


   XML_DWORD_OBJECT ("age", m_dwAge)

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;
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.



  • 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

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date