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

Creating the Main Form

The main form provides the user interface for the JumpStart application and consists of four elements.

Interface Element Description
Text1 A text box for entering the file name of the file to parse.
Command1 A button to start the parse process.
Command2 A button to exit the application.
Text2 A text box that displays the results of the parse operation.

In addition to providing the interface elements, the main form also provides code that creates the required instances of the classes and starts the parse process.

To The main form uses this code
Create an instance of the reader (SAXXMLReader) Dim reader As New SAXXMLReader
Create an instance of ContentHandlerImpl and set it to receive events from the reader Dim contentHandler As New ContentHandlerImpl
Set reader.contentHandler = contentHandler
Create an instance of ErrorHandlerImpl and set it to receive events from the reader Dim errorHandler As New ErrorHandlerImpl
Set reader.errorHandler = errorHandler
Start the parse process reader.parseURL (Text1.text)

Complete Code for the Main Form

The following is the complete code for the main form of the JumpStart application.

Option Explicit

Private Sub Command1_Click()

    Dim reader As New SAXXMLReader    'Reads the XML document
    Dim contentHandler As New ContentHandlerImpl 'Receives parsing events
    Dim errorHandler As New ErrorHandlerImpl        'Receive error events
    Text2.text = ""
    Set reader.contentHandler = contentHandler    'They work together
    Set reader.errorHandler = errorHandler        'They also work together
    On Error GoTo 10
    reader.parseURL (Text1.text)               'Parse the document

    Exit Sub                                   'That's all, folks!

10: Text2.text = Text2.text & "*** Error *** " & Err.Number _
                 & " : " & Err.Description
End Sub

Private Sub Command2_Click()
    End
End Sub

The Exit Sub and line marked 10: are required only if you want to handle processing outside the ErrorHandler. Otherwise, you can use On Error Resume Next.

To run the JumpStart application

  1. Download the JumpStart application, available from the Other ResourcesMSDN Code Center, to your computer.
  2. On your computer, double-click VBSAXExample.exe.
  3. In the File name box, type "Test.xml" and then click Parse. The JumpStart application parses the file and displays the results in the Text2 text box.

You can use this 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.