Share via


Step 2: Creating the MyContent Class (Windows Embedded CE 6.0)

1/6/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.

#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

Tasks

Tutorial: JumpStart for Creating a SAX2 Application with C++

Concepts

Step 1: Creating the Header File
Step 3: Creating the Main Program
Getting Started with SAX2
SAX2 Developer Guide