65.9K
CodeProject 正在变化。 阅读更多。
Home

从 XML 自动初始化 C++ 对象

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.25/5 (7投票s)

2008年4月14日

CPOL

1分钟阅读

viewsIcon

25980

downloadIcon

278

从 XML 文件自动初始化您的数据类。

引言

本文提供简单的类和宏,用于从 XML 文件简单地初始化 C++ 对象。这里的主要目标是替代 .INI 文件,但它也可以用于小型数据初始化需求。

背景

处理的 XML 数据只是一个包含(或不包含)属性的简单 XML 元素集合。

使用代码

提供了两个类

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

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

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

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

第一个类描述了要从 XML 初始化的一系列对象,第二个类是一个简单的对象(没有 XML 子元素)。

为了使用这些类,您需要根据数据的结构从它们派生您的对象。

例如

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

提供了一些 (但如果您需要更多功能,可以轻松添加更多)

DECLARE_XMLOBJ()

=> 在 .h 中,每次对象从 CFromXMLObject 派生并且具有子对象和/或属性时。

DECLARE_XMLOBJLIST()

=> 在 .h 中,每次对象从 CFromXMLObjectList 派生并且 具有子对象且 没有 属性时。

用于连接成员变量的宏(也可以轻松扩展)

最容易解释的方式是通过一个例子

Company.cpp

// [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()

Employee.cpp

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()

您被提供了一些宏,用于将 XML 属性连接到您的成员变量

  • XML_OBJ_MAP_STRING:简单地将属性存储在成员字符串变量中。
  • XML_OBJ_MAP_LONG:将属性转换为 long
  • XML_OBJ_MAP_DOUBLE:转换为 double
  • XML_OBJ_MAP_BOOL:转换为 BOOL
  • 我没有提供其他数据类型连接,但扩展这些提供的类型非常容易。

历史

  • 2008/04/14:首次在 CodeProject 上公开发布。
© . All rights reserved.