Click here to Skip to main content
13,006,530 members (59,347 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 11 Apr 2008

Portable C++ XML Serialization

, 2 Jan 2011
Rate this:
Please Sign up or sign in to vote.
An open C++ library to serialize objects as XML


XML serialization of objects is very important for a lot of applications.

C# provides an easy way to do this using the namespace System.Xml.Serialization.

However the standard C++ is poor to this feature.

Xmlser is an open source library that allows you to easily serialize/deserialize objects as XML.


You should know a little about the XPATH expressions. Only the basics.

Given an XML Document:

<Person first_name = "F">
	<last_name> L </last_name>

To select Person (which the root element of the XML document), use the XPATH expression: /Person.

To select first_name which is an attribute of Person: /Person/@first_name.

As you noticed @ is used to specify an attribute.

/Person/last_name is an XPATH expression to select the last_name element.

The asterix (or star '*') is used to say anything:

  • /Person/* is equivalent in this example to /Person/last_name
  • /Person/@* is equivalent in this example to /Person/@first_name

Using the Code

Public Classes

Only 3 classes are to be used, you can find them all in xmlser.h:

  1. XmlSerializable
  2. XmlReader
  3. XmlWriter

To make your objects XML serializable, you must inherit from XMLSerializable.

As you find in the source code test.cpp, the class Point2D is XMLSerializable:

class Point2D : public XmlSerializable 

Once inherited from XmlSerializable, a class should implement the two abstract members: writeXml and readXml. For the Point2D, we store as double its x and y coordinates, we should specify where (as XPATH expression) to place each of them:

void writeXml(XmlWriter& ws)
void readXml(XmlReader& rs)

The resulted XML to read/write is simliar to:

	<x> value of x as double </x>
	<y> value of y as double </y>

Put attention that the strings used while doing the serialization are unicode strings (must use the prefix L for ex: L"bla bla").


The expression syntax I used to serialize objects is a subset of the XPATH syntax, not any XPATH expression can be used, but an XPATH expression with the following constraints:

  • Always starting from the root "/" (absolute expression not relative one, XPATH allows relative ones).
  • An attribute can appear only as a leaf.
  • No XPATH functions are used.
  • Use the star only at the end of the expression, @* is not allowed.

To save the XML to disk, you can easily use the method saveTo having an XmlWriter object:

Point2D pt(11.3, 15.5);
XmlWriter writer;

In fact, an XmlWriter object is an: in-memory XML document accumulator.

You can dump it any time you want using the saveTo method.

To read the XML from disk as a Point2D object, you should use an XmlReader with the loadFrom method:

XmlReader reader;
Point2D pt;

Using Attributes

You might prefer to save x as an attribute, y as an element, in this case change the implementation of readXml and writeXml in the class Point2D to the following:

void writeXml(XmlWriter& ws)
void readXml(XmlReader& rs)

The resulted XML to read/write is similar to:

<Point2D x="value of x as double">
	<y> value of y as double </y>

XML Serialization for Composite Objects

Generally, an Object could be composed from another objects, this can be traduced using the XML format by putting the XML representation of the component object into the XML representation of the object composed by. For example, a rectangle is composed from 2 points. A rectangle object can be represented as XML according to the following template:


The highlighted parts are exactly corresponding to a Point2d each of them.

The class Rect2D should have 2 point2d as explicit members or as array.

As explicit members:

class Rect2D : public XmlSerializable
	Point2D topLeft, bottomRight;

As an Array:

class Rect2D : public XmlSerializable
    Point2D points[2];

The implementation of the abstract stubs readXml and writeXml is required.
In case of explicit members:

void readXml(XmlReader& rs)
void writeXml(XmlWriter& ws)

getXml and setXml methods provide here the reuse of the XmlSerializable Point2D objects serialization. In fact, topLeft and bottomRight members are Point2D and already have their way of read/write as XML. If members are defined as array:

void readXml(XmlReader& rs)
void writeXml(XmlWriter& ws)

Now, what if we think about an abbreviated template for the XML like the following:


where the first point is the top left, and the second is the bottom right one.

In this case, the code for read/write XML is:

void readXml(XmlReader& rs)
void writeXml(XmlWriter& ws)

You should use the * in the XPATH expression only in such cases, when you want to copy/paste another XML representation. The indices used in the getXml method (last parameter) are to specify that points[0] is the first in XML, point[1] is the second, since the indexing routine for "getter methods" is starting from 0. "setter methods" don't use any indices. Setters will never override the previous elements (in case of attributes, it will). It appends a next sibling element each time called on the same expression: I present here a tracing for the writeXml routine:

First call:

           <Point2D><x></x><y></y><Point2D> (points[0] goes here)

Second call:

           <Point2D><x></x><y></y><Point2D> (points[0] still here)
           <Point2D><x></x><y></y><Point2D> (points[1] goes here)

You find in test.cpp an example for a Polygon that is composed from an array of Points, it is easy and no need to explain it.

Demo Example

The demo example located in test.cpp lets you learn how to use basically the API to serialize objects.

3 XML files are produced in the "start in" folder of the demo: f.xml, rect2d.xml, and poly2d.xml.

Third Party (All What I Did is a Thin Wrapper Around)

I used the VTD-XML open source library (XimpleWare), C version with little update for customization and solving some memory leaks.

VTD-XML is a very efficient XML parser.

Points of Interest

I used a restricted XPATH format to accomplish the general XML serialization following a simple, and easy to use approach. The code I wrote is multi-platform, although the project presented is compiled in the Windows environment. Xmlser API can deal with any XML document produced by different applications, and convert it to objects. XmlSer API can be converted to Java because there is an available version of VTD-XML written in Java.


  • Initial version: April/11/2008


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Software Developer (Senior)
Lebanon Lebanon
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionYup Previous comments are correct: Don't waste your time: corrupts heap. Pin
bik toor28-Jul-13 19:40
memberbik toor28-Jul-13 19:40 
QuestionAsking about The Licence Pin
AmrThabet25-May-12 6:01
memberAmrThabet25-May-12 6:01 
AnswerRe: Asking about The Licence Pin
HZ_7918-Jun-12 0:52
memberHZ_7918-Jun-12 0:52 
GeneralRe: My vote of 1 Pin
tha_specializt3-Jan-11 2:09
membertha_specializt3-Jan-11 2:09 
Generala small bug [modified] Pin
tha_specializt9-Dec-10 22:37
membertha_specializt9-Dec-10 22:37 
GeneralRe: a small bug Pin
tha_specializt13-Dec-10 1:57
membertha_specializt13-Dec-10 1:57 
GeneralMissing some const correctness Pin
Seikilos10-Dec-08 21:40
memberSeikilos10-Dec-08 21:40 
GeneralRe: Missing some const correctness Pin
HZ_792-Jan-11 21:32
memberHZ_792-Jan-11 21:32 
Generaluse your code under VC++ 6.0 Pin
marcoooooo7-Aug-08 21:09
membermarcoooooo7-Aug-08 21:09 
GeneralRe: use your code under VC++ 6.0 Pin
dtran1110-Feb-09 13:34
memberdtran1110-Feb-09 13:34 
GeneralRe: use your code under VC++ 6.0 Pin
HZ_792-Jan-11 21:43
memberHZ_792-Jan-11 21:43 
GeneralThis code can't be licensed as indcated Pin
Rob Graham11-Apr-08 4:21
memberRob Graham11-Apr-08 4:21 
GeneralRe: This code can't be licensed as indcated [modified] Pin
HZ_7911-Apr-08 6:07
memberHZ_7911-Apr-08 6:07 
GeneralPeople might want to know Pin
Jim Crafton11-Apr-08 3:16
memberJim Crafton11-Apr-08 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 3 Jan 2011
Article Copyright 2008 by HZ_79
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid