Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The source code listed below demonstrates how to call the transformNode method and the transformNodeToObjectmethod in a C/C++ program. Specifically, the code performs the following steps:
Loads the XML data file (stocks.xml) into an XML DOM object (
pXMLDom).Loads the XSLT style sheet (stocks.xsl) into an XML DOM object (
pXSLDoc).Calls the
transformNode(pXSLDoc)method onpXMLDomto do the transformation, holds the result in a string (xmlStr), and prints the output to the console.Creates an XML DOM object (
pXMLOut) to hold the output of thetransformNodeToObjectmethod.Calls the
transformNodeToObjectmethod onpXMLDomto do the transformation; holds the resulting object inpXMLOut; prints out the XML result; and serializes the output object in an HTML file, stocks.htm.
C/C++ Source File (XSLTsmart.cpp)
#include <stdio.h>
#include <tchar.h>
#import <msxml6.dll>
// Macro that calls a COM method returning HRESULT value.
#define CHK_HR(stmt) do { hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)
void XSLTsmart()
{
MSXML2::IXMLDOMDocumentPtr pXMLDom;
MSXML2::IXMLDOMDocumentPtr pXSLDoc;
MSXML2::IXMLDOMDocumentPtr pXMLOut;
HRESULT hr = S_OK;
try
{
// Load the XML file.
if (FAILED(pXMLDom.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER)))
{
printf("Failed to instantiate an XML DOM.\n");
CHK_HR(E_FAIL);
}
pXMLDom->async = VARIANT_FALSE; // The default is true.
pXMLDom->validateOnParse = VARIANT_FALSE;
pXMLDom->resolveExternals = VARIANT_FALSE;
if(pXMLDom->load(L"stocks.xml") != VARIANT_TRUE)
{
printf("Failed to load stocks.xml:\n%s\n", (LPCSTR)pXMLDom->parseError->Getreason());
CHK_HR(pXMLDom->parseError->errorCode);
}
// Load the XSLT style sheet.
if (FAILED(pXSLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER)))
{
printf("Failed to instantiate an XML DOM.\n");
CHK_HR(E_FAIL);
}
pXSLDoc->async = VARIANT_FALSE; // The default is true.
pXSLDoc->validateOnParse = VARIANT_FALSE;
pXSLDoc->resolveExternals = VARIANT_FALSE;
if(pXSLDoc->load(L"stocks.xsl") != VARIANT_TRUE)
{
printf("Failed to load stocks.xsl:\n%s\n", (LPCSTR)pXMLDom->parseError->Getreason());
CHK_HR(pXSLDoc->parseError->errorCode);
}
// Transform the XSLT to an XML string.
_bstr_t xmlStr = pXMLDom->transformNode(pXSLDoc);
printf("Output from transformNode:\n%s\n", (LPCSTR)xmlStr);
// Instantiate a DOM for xmlOut object.
if (FAILED(pXMLOut.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER)))
{
printf("Failed to instantiate an XML DOM.\n");
CHK_HR(E_FAIL);
}
// Transform the XSLT to a DOM object.
CHK_HR(pXMLDom->transformNodeToObject(pXSLDoc, pXMLOut.GetInterfacePtr()));
printf("Output from transformNodeToObject:\n%s\n", (LPCSTR)pXMLOut->xml);
CHK_HR(pXMLOut->save(L"stocks.htm"));
printf("The above output is also saved in stocks.htm.\n");
}
catch (_com_error errorObject)
{
printf("Exception thrown, HRESULT: 0x%08x", errorObject.Error());
}
CleanUp:
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitialize(NULL);
if(SUCCEEDED(hr))
{
XSLTsmart();
CoUninitialize();
}
return 0;
}
To add the XSLT source code to the project
Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the new file XSLTsmart.cpp.
Copy the C/C++ source code above and paste it into the source file you just created.
Next, we'll add the resource files to the XSLT project.