The following example shows how easy it is to add XML Persistence to your classes, using rP-XML.
In this example, we create a hypothetical employee class which can be stored in XML.
All persistence-related code is shown in blue highlight:
#include <stdio.h>
#include "xmlpersist.h"
class Employee : public XMLPersistent
{
public:
char* m_pszName;
int m_iEmployeeNumber;
int m_iDepartment;
char* m_pszResume;
Employee()
{
m_pszName = NULL;
m_iEmployeeNumber = 0;
m_iDepartment = 0;
m_pszResume = NULL;
}
~Employee()
{
DeleteDynamicString(m_pszName);
DeleteDynamicString(m_pszResume);
}
DECLARE_XMLPERSIST_CLASS;
};
// The following is the "data exchange macro block".
// It describes the mapping between members of the Employee class
// and an XML tag we have chosen to call employee
BEGIN_XMLPERSIST_CLASS(Employee,employee)
XMLPERSIST_ITEM(m_pszName,name)
XMLPERSIST_ITEM(m_iEmployeeNumber,employee-number)
XMLPERSIST_ITEM(m_iDepartment,department)
BEGIN_XMLPERSIST_CONTENTS
XMLPERSIST_CONTENTS(m_pszResume)
END_XMLPERSIST
main(int argc, char* argv[])
{
Employee anEmployee;
XMLStorage xmlObject;
if (!xmlObject.ReadFile(&anEmployee, argv[1]))
{
printf ("read file failed: error code = %d;\nmessage = %s\n",
xmlObject.m_Exception.m_Code,
xmlObject.m_Exception.Message());
}
printf ("Employee Name: %s\nEmployee Number: %d\nDepartment: %d\nResume:\n%s\n",
anEmployee.m_pszName, anEmployee.m_iEmployeeNumber, anEmployee.m_iDepartment,
anEmployee.m_pszResume);
XMLPersistent::SetDynamicString(anEmployee.m_pszName, "John Smith");
anEmployee.m_iEmployeeNumber = 101;
anEmployee.m_iDepartment = 10;
XMLPersistent::SetDynamicString(anEmployee.m_pszResume,
"Resume of John Smith\n",
"Contact Information...\n"
"Career Summary...\n"
"Work History...\n"
"Education...\n"
"Interests...\n"
);
if (!xmlObject.WriteFile(&anEmployee, argv[1]))
{
printf ("write file failed: error code = %d; message = %s\n",
xmlObject.m_Exception.m_Code,
xmlObject.m_Exception.Message());
}
} |
If you were to build and run the above example, giving the name of a non-existent file, you would get the following results:
| Written to a file as named on the command line: | Written to standard output: |
<employee name="John Smith"
employee-number="101"
department="10">
Resume of John Smith
Contact Information...
Career Summary...
Work History...
Education...
Interests...
</employee> |
read file failed: error code = 11;
message = Failed to open filename. No such file or directory
Employee Name:
Employee Number: 0
Department: 0
Resume: |
| Note: the actual XML file may not appear exactly as shown; newlines have been added for clarity. |
If you were to re-run the example, giving the name of the file which was just created, you would get the following results:
| Written to a file as named on the command line: | Written to standard output: |
<employee name="John Smith" employee-number="101" department="10"> Resume of John Smith Contact Information... Career Summary... Work History... Education... Interests... </employee> |
Employee Name: John Smith Employee Number: 101 Department: 10 Resume: Resume of John Smith Contact Information... Career Summary... Work History... Education... Interests... |
Copyright © 2002 by rObjects. All rights reserved.