Click here to Skip to main content
15,881,687 members
Articles / Desktop Programming / ATL
Article

Automatic C++ object initialization from XML

Rate me:
Please Sign up or sign in to vote.
4.25/5 (7 votes)
13 Apr 2008CPOL1 min read 24.6K   259   21   3
Automate your data classes initialization from an XML file.

Introduction

This article provides simple classes and macros for simple C++ object initialization from an XML file. Here, the main goal is to achieve .INI files substitution, but it can be used for small data initialization needs as well.

Background

The XML data beeing processed is just a simple collection of XML elements containing (or not) attributes.

Using the code

Two classes are provided:

C++
class CFromXMLObjectList : public CObList
{
public:
    CFromXMLObjectList();
    virtual ~CFromXMLObjectList();

    BOOL FillFromXMLFile(CString xmlpath);
    virtual BOOL FillFromXML(IXMLDOMNode* node)=0;
};

and:

C++
class CFromXMLObject : public CObject
{
public:
    CFromXMLObject();
    virtual ~CFromXMLObject();

    BOOL FillFromXMLFile(CString xmlpath,CString elementName);
    virtual BOOL FillFromXML(IXMLDOMNode* node)=0;
};

The first one describes a list of objects to initialize from XML, and the second one is a simple object (without XML children).

In order to use these classes, you have to derive your objects from them, depending on the structure of your data.

For example:

C++
class CCompany : public CFromXMLObjectList  
{
public:
    CCompany();
    virtual ~CCompany();

    DECLARE_XMLOBJ()
    CString Dump();

    // [14/4/2008 Alexandre GRANVAUD] this is example,
    // so few items needed actually
private:
    CString        m_strName;
    CString        m_strAdress;
};
class CEmployee : public CFromXMLObject  
{
public:
    CEmployee();
    virtual ~CEmployee();

    DECLARE_XMLOBJ()

    CString Dump();
private:
    CString m_strName;
    long m_lBirthYear;
    long m_lBirthMonth;
    long m_lBirthDay;
};

A few macros are provided (but you can easily add more if you need more features):

C++
DECLARE_XMLOBJ()

=> in .h, every time an object is derived from CFromXMLObject and has sub-objects and/or attributes.

C++
DECLARE_XMLOBJLIST()

=> in .h, every time an object is derived from CFromXMLObjectList and has only sub-objects and no attributes.

Macros for connecting member variables (can also be extended easily):

The easiest way to explain is by an example:

In Company.cpp:

C++
// [14/4/2008 Alexandre GRANVAUD]
// Here is the data connection to data members of the class
BEGIN_XMLOBJ(CCompany)
    XML_OBJ_MAP_STRING("Name",m_strName)    
    XML_OBJ_MAP_STRING("Adress",m_strAdress)
    // [14/4/2008 Alexandre GRANVAUD] always end
    // the member connection before connecting sub-objects
    INNER_END_XMLOBJ()
    // [14/4/2008 Alexandre GRANVAUD] always
    // link to sub-objects at the end of parsing
    BEGIN_INNER_OBJLIST()
        XMLOBJLIST_MAP("EMPLOYEE",CEmployee)
    // [14/4/2008 Alexandre GRANVAUD] IMPORTANT ! if there's
    // an inner list, always end with END_XMLOBJLIST,
    // even in a BEGIN_XMLOBJ block
END_XMLOBJLIST()

In Employee.cpp:

C++
BEGIN_XMLOBJ(CEmployee)
    // [14/4/2008 Alexandre GRANVAUD] connect
    // the attribute Name to member m_strName
    XML_OBJ_MAP_STRING("Name",m_strName)
    // [14/4/2008 Alexandre GRANVAUD] connect the attribute
    // Year to member m_lBirthYear and convert it to long
    XML_OBJ_MAP_LONG("Year",m_lBirthYear)
    XML_OBJ_MAP_LONG("Month",m_lBirthMonth)
    XML_OBJ_MAP_LONG("Day",m_lBirthDay)
END_XMLOBJ()

You are being provided a few macros to connect an XML attribute to your member variable:

  • XML_OBJ_MAP_STRING: simply stores the attribute in a member string variable.
  • XML_OBJ_MAP_LONG: converts the attribute to long.
  • XML_OBJ_MAP_DOUBLE: converts to double.
  • XML_OBJ_MAP_BOOL: converts to BOOL.
  • I've not provided other data type connections, but it's very easy to expand those provided.

History

  • 2008/04/14: first public version on CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralA better way Pin
Brian Aberle20-Aug-09 5:51
professionalBrian Aberle20-Aug-09 5:51 
GeneralMight be useful but... PinPopular
juggler14-Apr-08 4:32
juggler14-Apr-08 4:32 
GeneralRe: Might be useful but... Pin
Alexandre GRANVAUD14-Apr-08 20:47
Alexandre GRANVAUD14-Apr-08 20:47 

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.