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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Specialization and efficiency are always in need. Whether it's replacing an aging roof, getting a haircut, or tuning up a car, most seek the assistance of trusted experts. The same is true in the business world, where an increasing number of companies are seeking the help of others to administer their IT systems and services. This special edition of Unleashing IT highlights a new breed of IT caretaker -- Cisco Powered service providers -- and the business advantages and operational efficiencies they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds