Click here to Skip to main content
15,896,453 members
Articles / Desktop Programming / ATL

Using XSLT to Render XML Data Returned by a COM Object

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
12 Sep 20016 min read 138.5K   885   32  
This is a 3-tier application. A COM component returns SQL data in XML format, then using XSLT to transform to HTML at client side. By using XML, all the data can be sort, split into pages locally, that will give users a quick response!
// xmlrsobj.cpp : Implementation of Cxmlrsobj
#include "stdafx.h"
#include "Xmlrs.h"
#include "xmlrsobj.h"

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
    named_guids no_namespace rename("EOF", "EndOfFile")

inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};

bool GetRowsOK(_RecordsetPtr pRstTemp,int intNumber,
        _variant_t& avarData);

/////////////////////////////////////////////////////////////////////////////
// Cxmlrsobj

// global variable save the recordset point of Employees table
_RecordsetPtr pRstEmployees = NULL;

STDMETHODIMP Cxmlrsobj::ConnectDB(BSTR bstrConn)
{
	// Get the Innstance of Recordset
    TESTHR(pRstEmployees.CreateInstance(__uuidof(Recordset)));

	// Open the recordset by the specific SELECT
    pRstEmployees->Open("SELECT fName, lName, hire_date "
        "FROM Employee ORDER BY lName", 
		bstrConn, // Connection string comes from the ASP client
        adOpenStatic, adLockReadOnly, adCmdText);

	return S_OK;
}

STDMETHODIMP Cxmlrsobj::GetXmlrs(BSTR *bstrXmlrs)
{
	_bstr_t bstrTmp;

	// Wrap the Recordset into an XML format
	bstrTmp = "<Recordset>";

	while (true)
	{
		bstrTmp += "<Record>";
		bstrTmp += "<fName>";
		bstrTmp += (_bstr_t)pRstEmployees->Fields->Item["fName"]->Value;
		bstrTmp += "</fName><lName>";
		bstrTmp += (_bstr_t)pRstEmployees->Fields->Item["lName"]->Value;
		bstrTmp += "</lName><hire_date>";
		bstrTmp += (_bstr_t)pRstEmployees->Fields->Item["hire_date"]->Value;
		bstrTmp += "</hire_date>";
		bstrTmp += "</Record>";
		pRstEmployees->MoveNext();
		if (pRstEmployees->EndOfFile)
		{
			break;
		}
	}

	bstrTmp += "</Recordset>";

	*bstrXmlrs = bstrTmp.copy();

	return S_OK;
}

STDMETHODIMP Cxmlrsobj::CloseDB()
{
	// Close the database
    pRstEmployees->Close();

	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
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions