Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

After you create the MyContent.h file, the next step is to implement the ContentHandler class, named MyContent.

Example

The following code example shows how to implement ContentHandler.

Note:
To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.
Copy Code
#include "stdafx.h"
#include "MyContent.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MyContent::MyContent()
{
   idnt = 0;
}

MyContent::~MyContent()
{

}

HRESULT STDMETHODCALLTYPE MyContent::startElement( 
			/* [in] */ wchar_t __RPC_FAR *pwchNamespaceUri,
			/* [in] */ int cchNamespaceUri,
			/* [in] */ wchar_t __RPC_FAR *pwchLocalName,
			/* [in] */ int cchLocalName,
			/* [in] */ wchar_t __RPC_FAR *pwchQName,
			/* [in] */ int cchQName,
			/* [in] */ ISAXAttributes __RPC_FAR *pAttributes)
{
   HRESULT hr = S_OK;
   int l;
   printf("\n%*s",3 * idnt++, "");
	prt(L"<%s",pwchLocalName,cchLocalName);
   pAttributes->getLength(&l);
   for ( int i=0; i<l; i++ ) {
	wchar_t * ln, * vl; int lnl, vll;
	pAttributes->getLocalName(i,&ln,&lnl); 
	prt(L" %s=", ln, lnl);
	pAttributes->getValue(i,&vl,&vll);
	prt(L"\"%s\"", vl, vll);
   }
   printf(">");

   // A little example, how to abort parse
   if ( wcsncmp(pwchLocalName,L"qu",2) == 0 ) {
	printf("\n<qu> tag encountered, parsing aborted.");
	hr = E_FAIL;
   }

	return hr;
}
	
	 
HRESULT STDMETHODCALLTYPE MyContent::endElement( 
			/* [in] */ wchar_t __RPC_FAR *pwchNamespaceUri,
			/* [in] */ int cchNamespaceUri,
			/* [in] */ wchar_t __RPC_FAR *pwchLocalName,
			/* [in] */ int cchLocalName,
			/* [in] */ wchar_t __RPC_FAR *pwchQName,
			/* [in] */ int cchQName)
{
   printf("\n%*s",3 * --idnt, "");
	prt(L"</%s>",pwchLocalName,cchLocalName);
	return S_OK;
}

HRESULT STDMETHODCALLTYPE MyContent::startDocument()
{
	printf("<?xml version=\"1.0\" ?>");
	return S_OK;
}
	
void MyContent::prt(
			/* [in] */ const wchar_t * pwchFmt,
			/* [in] */ const wchar_t __RPC_FAR *pwchVal,
			/* [in] */ int cchVal)
{
	static wchar_t val[1000];
	cchVal = cchVal>999 ? 999 : cchVal;
	wcsncpy( val, pwchVal, cchVal ); val[cchVal] = 0;
	wprintf(pwchFmt,val);
}

See Also