Implementing the ContentHandler (Visual Basic)

MSXML 5.0 SDK

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

Implementing the ContentHandler (Visual Basic)

For the JumpStart application, you create the ContentHandler by adding a class that implements the IVBSAXContentHandler interface. Although creating a class may sound complex, Microsoft® Visual Basic® automates most of the process.

Opening a New Project

Before you can create a class based on the IVBSAXContentHandler interface, you must create a new project and then reference that project to Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office.

To create a new project

  • Open Visual Basic 6.0 and, in the New Project dialog box, double-click Standard EXE.

Referencing MSXML

You must now instruct your application to use the MSXML2 type library provided with MSXML 5.0.

To create a reference to MSXML 5.0

  1. On the Project menu, click References.
  2. In the Available References list, select Microsoft XML, v5.0, and then click OK.

Creating and Implementing the Handler Class

You are now ready to create and implement the new ContentHandler class.

To create a new class

  1. On the Project menu, click Add Class Module.
  2. In the Add Class Module dialog box, double-click Class Module.
  3. On the View menu, select Properties Window.
  4. In the Properties Window, enter "ContentHandlerImpl" for the Name property.

To implement the ContentHandlerImpl Class

  1. In the Class Window, type "Implements", and then press the space bar.

    A drop down list appears with the all the available classes. (This list includes interfaces that are actually abstract classes.)

  2. In the drop-down list, double-click IVBSAXContentHandler. This is the interface you implement to create the ContentHandlerImpl class.
  3. In the Object drop-down list on the left side of the Class Window, click IVBSAXContentHandler.

    This automatically adds code to the Class Window.

  4. In the Procedure drop-down list on the right-hand side of the Class Window, select the IVBSAXContentHandler method or property that you want to add to this class. (You have to select each method even though you do not need to add code to each one.)

    For example, select startElement to add this method to the ContentHandlerImpl class. (When parsing an XML document, the reader will invoke this method each time it encounters a new element in the document.) After you select startElement from the list, the following code appears in the Class Window:

    Private Sub IVBSAXContentHandler_startElement _
                             (strNamespaceURI As String, _
                              strLocalName As String, _
                              strQName As String, _
                              ByVal oAttributes As MSXML2.IVBSAXAttributes)
    End Sub

  5. To enable the method or event selected in Step 4 to handle the corresponding event, you need to add the appropriate code to that method or property.

    For the startElement method added in Step 4, enter the code required to make the method look like this:

    Private Sub IVBSAXContentHandler_startElement _
                              (strNamespaceURI As String, _
                               strLocalName As String, _
                               strQName As String, _
                               ByVal attributes As MSXML2.IVBSAXAttributes)
        Dim i As Integer
        
        Form1.Text2.text = Form1.Text2.text & "<" & strLocalName
       
        For i = 0 To (attributes.length - 1)
            Form1.Text2.text = Form1.Text2.text & " " _
            & attributes.getLocalName(i) & "=""" _
            & attributes.getValue(i) & """"
        Next
    
        Form1.Text2.text = Form1.Text2.text & ">"
    
        If strLocalName = "qu" Then
            Err.Raise vbObjectError + 1, "ContentHandler.startElement", _
                                         "Found element <qu>"
        End If
    
    End Sub

  6. To handle additional events passed by the reader, repeat steps 4 and 5 for each method or property that you want to add to the ContentHandlerImpl class.

Complete Code for the ContentHandlerImpl Class

The preceding instructions explain how to create a new project with a reference to Microsoft XML, v5.0, and how to create a ContentHandler class called ContentHandlerImpl by implementing the IVBSAXContentHandler interface. The following shows the complete code for the ContentHandlerImpl class.

Option Explicit

Implements IVBSAXContentHandler

Private Sub IVBSAXContentHandler_startElement _
                     (strNamespaceURI As String, _
                      strLocalName As String, _
                      strQName As String, _
                      ByVal attributes As MSXML2.IVBSAXAttributes)
    
    Dim i As Integer
    
    Form1.Text2.text = Form1.Text2.text & "<" & strLocalName
   
    For i = 0 To (attributes.length - 1)
        Form1.Text2.text = Form1.Text2.text & " " & _
                           attributes.getLocalName(i) & "=""" & _
                           attributes.getValue(i) & """"
    Next

    Form1.Text2.text = Form1.Text2.text & ">"

    If strLocalName = "qu" Then
        Err.Raise vbObjectError + 1, "ContentHandler.startElement", _
                                     "Found element <qu>"
    End If

End Sub

Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, _
                                            strLocalName As String, _
                                            strQName As String)
    
    Form1.Text2.text = Form1.Text2.text & "</" & strLocalName & ">"

End Sub

Private Sub IVBSAXContentHandler_characters(text As String)
    text = Replace(text, vbLf, vbCrLf)
    Form1.Text2.text = Form1.Text2.text & text
    
End Sub

Private Property Set IVBSAXContentHandler_documentLocator(ByVal RHS As _
                                                    MSXML2.IVBSAXLocator)

End Property

Private Sub IVBSAXContentHandler_endDocument()

End Sub

Private Sub IVBSAXContentHandler_endPrefixMapping(strPrefix As String)

End Sub

Private Sub IVBSAXContentHandler_ignorableWhitespace(strChars As String)

End Sub

Private Sub IVBSAXContentHandler_processingInstruction(target As String, _
                                                       data As String)
    
    Form1.Text2.text = Form1.Text2.text & "<?" & target & " " _
                       & data & ">"

End Sub

Private Sub IVBSAXContentHandler_skippedEntity(strName As String)

End Sub

Private Sub IVBSAXContentHandler_startDocument()

End Sub

Private Sub IVBSAXContentHandler_startPrefixMapping(strPrefix As String, _
                                                    strURI As String) 

End Sub

As you can see from the sample code, not all methods or properties have custom code added to them. When you implement a handler for the Simple API for XML (SAX2), you can add only the methods or properties that you need.

In the next section, you create the ErrorHandler for the JumpStart application.