Modify SaxJumpStart.cpp and stdafx.h

 

In this topic, you will create the SaxJumpStart.cpp file, which contains the _tmain function, and the stdafx.h file, which includes required headers.

The _tmain function:

  • Provides a command prompt interface.

  • Creates a parser by instantiating a class that implements the SAXXMLReader interface.

  • Creates a ContentHandler by instantiating the MyContent class.

  • Registers the ContentHandler with the parser.

Source Listing for SaxJumpStart.cpp

When you created your new project, Microsoft Visual Studio created the file SaxJumpStart.cpp that has an empty _tmain function. Modify SaxJumpStart.cpp so that it contains the following code:

// SaxJumpStart.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "MyContent.h"
#include "SAXErrorHandlerImpl.h"

int _tmain(int argc, _TCHAR* argv[])
{
    if (argc<2) {
        printf("\nTo run, enter\n\tSaxJumpStart file:///drive:/path/file.xml\n\n");
        return 0;   // Need URL to read
    }

    CoInitialize(NULL); 
    ISAXXMLReader* pRdr = NULL;

    HRESULT hr = CoCreateInstance(
                                __uuidof(SAXXMLReader60), 
                                NULL, 
                                CLSCTX_ALL, 
                                __uuidof(ISAXXMLReader), 
                                (void **)&pRdr);

    if(!FAILED(hr)) 
    {
        MyContent * pMc = new MyContent();
        hr = pRdr->putContentHandler(pMc);

        // Set error handler
        SAXErrorHandlerImpl * pEc = new SAXErrorHandlerImpl();
        hr = pRdr->putErrorHandler(pEc);

        printf("\nParsing document: %S\n", argv[1]);
        
        hr = pRdr->parseURL(argv[1]);
        printf("\nParse result code: %08x\n\n",hr);
    
        pRdr->Release();
    }
    else 
    {
        printf("\nError %08X\n\n", hr);
    }

    CoUninitialize();
    return 0;
}

We are now ready to create the rest of the files that comprise the application.

Source Listing for stdafx.h

When you created your new project, Microsoft Visual Studio created the file stdafx.h that includes required headers. Modify stdafx.h so that it contains the following code:

You are now ready to create the rest of the files that comprise the application.