ContentHandlerImpl Class

MSXML 5.0 SDK

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

ContentHandlerImpl Class

The ContentHandlerImpl class:

  • Implements the IVBSAXContentHandler and IVBSAXErrorHandler interfaces. The methods of these interfaces receive events thrown by the reader. You must add all methods for the implemented interfaces to the ContentHandlerImpl class.
  • Declares public variables for IVBSAXContentHandler and IVBSAXErrorHandler interfaces. The oContentHandler and oErrorHandler variables are set as properties of the ContentHandler object in the form and enable the writer to receive ContentHandler and ErrorHandler events.
  • Declares a FilterCriteria variable that holds the element name that serves as the criteria for the filter. Also declares a FilterTrue variable, a Boolean flag used to indicate whether the specified element is within scope.
  • Declares an errorHappen variable, a Boolean flag that indicates whether the ErrorHandler has thrown a fatal error message. This prevents the ErrorHandler procedure in the form from overwriting the error message thrown by the error handler
  • Contains If statements and method calls that push content to the result fragment when the specified element is within scope.

To create the 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, for the Name property, type "ContentHandlerImpl".

To implement an interface

  1. In the code window, type "Implements" and the name of the interface, for example:
    Implements IVBSAXContentHandler
    Implements IVBSAXErrorHandler
  2. In the left-hand drop-down in the code window, select the interface from the list.
  3. In the right-hand drop-down in the code window, you can implement the methods for the interface by selecting them from the list.
    Note   You must implement all methods for the implemented interfaces.

Complete Code for ContentHandlerImpl

Add the following code to the class.

Note   If you already added the Implements statements, you can simply copy the following code before the first Implements statement.

The essential methods used for the filter are discussed in the following topic, Essential Handler Methods.

Option Explicit
Implements IVBSAXContentHandler
Implements IVBSAXErrorHandler
'Declare a variable for setting the writer to the content handler.
Public oContentHandler As IVBSAXContentHandler
'Declare a variable for setting the writer to the error handler.
Public oErrorHandler As IVBSAXErrorHandler
'Flag to indicate if the error handler has thrown a fatal error.
Public errorHappen As Boolean
'Flag to indicate if the element is in scope.
Dim FilterTrue As Boolean
'Declare a string to hold the element name.
Dim FilterCriteria As String

Private Sub IVBSAXContentHandler_characters(strChars As String)
    If FilterTrue Then
           oContentHandler.characters strChars
    End If
End Sub

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

Private Sub IVBSAXContentHandler_endDocument()
    
End Sub

Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, _
                                            strLocalName As String, _
                                            strQName As String)
    If FilterTrue Then
         oContentHandler.endElement strNamespaceURI, strLocalName, _
                                    strQName
    End If
    If strLocalName = FilterCriteria Then
        FilterTrue = False
    End If
End Sub

Private Sub IVBSAXContentHandler_endPrefixMapping(strPrefix As String)

End Sub

Private Sub IVBSAXContentHandler_ignorableWhitespace(strChars As String)

End Sub

Private Sub IVBSAXContentHandler_processingInstruction(strTarget As _
                                                       String, _
                                                       strData As String)

End Sub

Private Sub IVBSAXContentHandler_skippedEntity(strName As String)
    
End Sub

Private Sub IVBSAXContentHandler_startDocument()
   
End Sub

Private Sub IVBSAXContentHandler_startElement(strNamespaceURI As String, _
                                              strLocalName As String, _
                                              strQName As String, _
                                              ByVal oAttributes As _
                                              MSXML2.IVBSAXAttributes)
    If strLocalName = FilterCriteria Then
        FilterTrue = True
    End If
    If FilterTrue Then
        oContentHandler.startElement strNamespaceURI, strLocalName, _
                                     strQName, oAttributes
    End If
End Sub

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

End Sub

Private Sub Initialize()
    errorHappen = False
    FilterTrue = False
End Sub

Private Sub IVBSAXErrorHandler_error(ByVal oLocator As _
                                     MSXML2.IVBSAXLocator, _
                                     strErrorMessage As String, _
                                     ByVal nErrorCode As Long)

End Sub

Private Sub IVBSAXErrorHandler_fatalError(ByVal oLocator As _
                                          MSXML2.IVBSAXLocator, _
                                          strErrorMessage As String, _
                                          ByVal nErrorCode As Long)
    Form1.Text1.Text = strErrorMessage & nErrorCode
    errorHappen = True
End Sub

Private Sub IVBSAXErrorHandler_ignorableWarning(ByVal oLocator As _
                                                MSXML2.IVBSAXLocator, _
                                                strErrorMessage As _
                                                String, _
                                                ByVal nErrorCode As Long)
End Sub

Public Sub SetFilterCriteria(elementname)
    FilterCriteria = elementname
End Sub

See Also

Essential Handler Methods | IMXWriter Interface | MXXMLWriter CoClass | ISAXContentHandler Interface | ISAXErrorHandler Interface