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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds