Click here to Skip to main content
15,895,011 members
Articles / Desktop Programming / ATL

XMLize Your Class

Rate me:
Please Sign up or sign in to vote.
4.17/5 (6 votes)
2 Apr 2001 244.4K   2K   40  
This article shows how to serialize a class into XML string.
// Employee.cpp : Implementation of CEmployee
/**********************************************************************************
//
// Copyright (C) Dhandapani Ammasai (dammasai@yahoo.com)
//
// This code can be freely used or modified for non commercial use provided that
// this copyright notice and license appear on all copies of the software.
//
**********************************************************************************/

#include "stdafx.h"
#include "XMLizeEmployee.h"
#include "Employee.h"

/////////////////////////////////////////////////////////////////////////////
// CEmployee

//Constructor
CEmployee::CEmployee()
{
	m_lId = 0;
	m_szFirstName.Empty();
	m_szLastName.Empty();
	m_szAddress.Empty();

	InitXMLNodeVariableMap();

	HRESULT hRes = CComObject<CDept>::CreateInstance(&m_pDept);
	ATLASSERT(SUCCEEDED(hRes));
	m_pDept->AddRef();

}

//Destructor
CEmployee::~CEmployee()
{
	m_pDept->Release();
}

//Set <XML Node, Variable> mapping 
void CEmployee::InitXMLNodeVariableMap()
{
	START_XML_NODE_VAR_MAP()
		//Specify xml node name, data type and the variable to hold the node value
		XML_NODE_VAR_MAP(NODE_ID, VT_I4, &m_lId)
		XML_NODE_VAR_MAP(NODE_FIRSTNAME, VT_BSTR, &m_szFirstName)
		XML_NODE_VAR_MAP(NODE_LASTNAME, VT_BSTR, &m_szLastName)
		XML_NODE_VAR_MAP(NODE_ADDRESS, VT_BSTR, &m_szAddress)
	END_XML_NODE_VAR_MAP()
}

BOOL CEmployee::FillAttribute(MSXML::IXMLDOMNode* pXMLNode)
{
	ATLASSERT(pXMLNode);

	return FillAttributeHelper(pXMLNode,XML_NODEVARMAP_STRUCT, XML_NODE_VAR_MAP_COUNT);
}

long CEmployee::Save(MSXML::IXMLDOMNode* pCurrentNode)
{
	//Empty DOM tree
	EmptyDOM();

	//Block of code below will build xml tree
	//
	//MSXML::IXMLDOMNodePtr pCurrentNode = NULL;
	XML_SET_CURRENTNODE(pCurrentNode)
	START_XML()
		START_XML_ELEMENT(NODE_EMPLOYEE)
			XML_ELEMENT_LONG(NODE_ID, m_lId)
			XML_ELEMENT_STR(NODE_FIRSTNAME, m_szFirstName)
			XML_ELEMENT_STR(NODE_LASTNAME, m_szLastName)
			XML_ELEMENT_STR(NODE_ADDRESS, m_szAddress)
			m_pDept->Save(XML_GET_CURRENTNODE); //XMLize Dept
		END_XML_ELEMENT()
	END_XML()
	return S_OK;
}

long CEmployee::ReadContainedObject(MSXML::IXMLDOMNode* pNode, BOOL& bContainedObjectFound)
{
	ATLASSERT(pNode);
	bContainedObjectFound = FALSE;

	BSTR szTempNodeName;
	pNode->get_nodeName(&szTempNodeName);
	_bstr_t szNodeName(szTempNodeName, FALSE);

	if ( szNodeName == _bstr_t("Dept") ) //Load Dept Object
	{
		bContainedObjectFound = TRUE;
		return m_pDept->LoadXMLNode(pNode);
	}
	return S_OK;
}


//Description:
//		Serialize employee class in to XML String
//Parameters:
//		pXMLStr - Output param. Will hold the resultant XML String
//Returns:
//		S_OK if successful;
STDMETHODIMP CEmployee::XMLize(BSTR *pXMLStr)
{
	ATLASSERT(pXMLStr);
	if (pXMLStr == NULL)
	{
		return E_POINTER;
	}

	Save(m_pXMLDOMDoc);

	//Get XML string from DOM
	BSTR tmpXMLStr;
	HRESULT hResult = m_pXMLDOMDoc->get_xml(&tmpXMLStr);
	if ( !SUCCEEDED(hResult) )
	{
		ATLASSERT(FALSE);
		return hResult;
	}

	_bstr_t XMLStr(tmpXMLStr, FALSE);
	*pXMLStr = XMLStr.copy();

	return S_OK;
}

//Description:
//	Deserialize employee class from XML String
//Parameters:
//	XMLStr - Input param. XML String from which the employee object will be constructed.
//Returns:
//	S_OK if successful;

STDMETHODIMP CEmployee::deXMLize(BSTR XMLStr)
{
	long lRet = LoadXML(m_pXMLDOMDoc, XMLStr);

	if (  lRet != S_OK )
	{
		return E_FAIL;
	}

	return S_OK;
}


// Get/Set methods
STDMETHODIMP CEmployee::get_Id(long *pVal)
{
	*pVal = m_lId;

	return S_OK;
}

STDMETHODIMP CEmployee::put_Id(long newVal)
{
	m_lId = newVal;

	return S_OK;
}

STDMETHODIMP CEmployee::get_FirstName(BSTR *pVal)
{
	*pVal = m_szFirstName.Copy();

	return S_OK;
}

STDMETHODIMP CEmployee::put_FirstName(BSTR newVal)
{
	m_szFirstName = newVal;

	return S_OK;
}

STDMETHODIMP CEmployee::get_LastName(BSTR *pVal)
{
	*pVal = m_szLastName.Copy();

	return S_OK;
}

STDMETHODIMP CEmployee::put_LastName(BSTR newVal)
{
	m_szLastName = newVal;

	return S_OK;
}

STDMETHODIMP CEmployee::get_Address(BSTR *pVal)
{
	*pVal = m_szAddress.Copy();

	return S_OK;
}

STDMETHODIMP CEmployee::put_Address(BSTR newVal)
{
	m_szAddress = newVal;

	return S_OK;
}

STDMETHODIMP CEmployee::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IEmployee
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}


STDMETHODIMP CEmployee::get_Dept(LPDISPATCH *pVal)
{
	LPDISPATCH lpdisp;
	m_pDept->QueryInterface(IID_IDispatch, (void**)&lpdisp);

	ATLASSERT(lpdisp != NULL);

	*pVal = lpdisp;

	return S_OK;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
India India
Dhandapani Ammasai(Dan in short) is a software delivery manager at a top tier IT company in India.

Comments and Discussions