MyValidator Class (SAX Validator)

MSXML 5.0 SDK

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

MyValidator Class (SAX Validator)

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 "MyValidator".

To implement an interface

  1. In the code window, type "Implements" and the name of the interface, for example:
    Implements IVBSAXContentHandler
    Implements IMXSchemaDeclHandler
    Implements IVBSAXErrorHandler
    Implements IVBSAXLocator
  2. In the left-hand drop-down list in the code window, select the interface.
  3. In the right-hand drop-down list 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 MyValidator

Add the following code to the class.

Note   If you already added the Implements statements, you can simply copy the following code and paste it before the first Implements statement.
Option Explicit
'Implement SAX interfaces.
Implements IVBSAXContentHandler
Implements IMXSchemaDeclHandler
Implements IVBSAXErrorHandler
Implements IVBSAXLocator
'Declare a module-scope variable for setting the locator.
Private oLocator As IVBSAXLocator

Private Sub IMXSchemaDeclHandler_schemaElementDecl(ByVal oSchemaElement As MSXML2.ISchemaElement)
End Sub

Private Sub IVBSAXContentHandler_characters(strChars As String)
End Sub

Private Property Set IVBSAXContentHandler_documentLocator(ByVal RHS As MSXML2.IVBSAXLocator)
  Set oLocator = RHS
End Property

Private Sub IVBSAXContentHandler_endDocument()
  frmMain.txtResults = frmMain.txtResults + "File is valid."
End Sub

Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, strLocalName As String, strQName As String)
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)
End Sub

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

Private Sub IVBSAXErrorHandler_error(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long)
   WriteErrorToResults "Error", strErrorMessage, _
         nErrorCode, oLocator.lineNumber, oLocator.columnNumber
End Sub

Private Sub IVBSAXErrorHandler_fatalError(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long)
   WriteErrorToResults "Fatal error", strErrorMessage, _
         nErrorCode, oLocator.lineNumber, oLocator.columnNumber
End Sub

Private Sub IVBSAXErrorHandler_ignorableWarning(ByVal oLocator As MSXML2.IVBSAXLocator, strErrorMessage As String, ByVal nErrorCode As Long)
   WriteErrorToResults "Ignorable warning", strErrorMessage, _
         nErrorCode, oLocator.lineNumber, oLocator.columnNumber
End Sub

Private Property Get IVBSAXLocator_columnNumber() As Long
End Property

Private Property Get IVBSAXLocator_lineNumber() As Long
End Property

Private Property Get IVBSAXLocator_publicId() As String
End Property

Private Property Get IVBSAXLocator_systemId() As String
End Property

Private Function WriteErrorToResults(strLabel As String, _
   strDescription As String, ByVal ErrCode As Long, _
   Line As Long, Column As Long)
   frmMain.txtResults = _
        strLabel + ": (" + CStr(ErrCode) + ") " + _
        strDescription & "at " + "line " + _
        Str(Line) + ", column " + _
        Str(Column) + vbCrLf
End Function

See Also

Validate Documents Using SAX | Overview of the SAX Validator Application | Application Form (SAX Validator) | Sample XSD Schema File (SAX Validator) | Run the Application (SAX Validator) | How the SAX Validator Application Works