Creating the Main Program

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - SAX2 Developer's Guide

Creating the Main Program

In this topic, you create a main program that:

  • 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.

Complete Code for the Main Program

The following is the code for the main program.

#include "stdafx.h"

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

int main(int argc, char* argv[])
{
   if (argc<2) {
      printf("\nTry something like\n\ttestSax_
      file:///drive:/path/file.xml\nGood luck!\n");
      return 0;   // Need URL to read
   }

   CoInitialize(NULL); 
   ISAXXMLReader* pRdr = NULL;

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

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

      // No sense to do so in this example, just an illustration how to _
         set other handlers
      //===================================================================================
       SAXErrorHandlerImpl * pEc = new SAXErrorHandlerImpl();
       hr = pRdr->putErrorHandler(pEc);
      // SAXDTDHandlerImpl * pDc = new SAXDTDHandlerImpl();
      // hr = pRdr->putDTDHandler(pDc);


      static wchar_t URL[1000];
      mbstowcs( URL, argv[1], 999 );
      wprintf(L"\nParsing document: %s\n", URL);
      
      hr = pRdr->parseURL(URL);
      printf("\nParse result code: %08x\n\n",hr);
   
      pRdr->Release();
   }
   else 
   {
      printf("\nError %08X\n\n", hr);
   }

   CoUninitialize();
   return 0;
}

To run the JumpStart application

  1. Download the JumpStart application, available from the Other ResourcesMSDN Code Center, to your computer.
  2. From the command prompt, type the following:
    pathname\debug\cppsaxsample.exe test.xml.

    where pathname is the name of the folder to which you downloaded the JumpStart application files. The test.xml file is a test file provided with the download. The parsed test.xml file should appear in the command prompt window.

You can use this brief introduction to the Simple API for XML (SAX2) as a starting point for writing your own applications. For more information about the Microsoft® XML Core Services (MSXML) 5.0 for Microsoft Office implementation of the SAX2 interfaces, see the SAX2 Reference.