To get started, create a new project and then reference Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office.
To create a new project
To create a reference to MSXML 5.0
Now build the user interface for the SAX validator application. Add the following controls to the form.
For your application form to work with the sample code provided in the next section, you must set the properties in the following table.
Control | Property | Setting |
---|---|---|
Form | Name
Caption |
frmMain
"SAX Validator" |
Text1 | Name
Text |
txtInputFile
"\books.xml" |
Text2 | Name
Text |
txtSchemaFile
"\books.xsd" |
Text3 | Name
MultiLine Text |
txtResults
True "" |
Command1 | Name
Caption |
cmdValidate
"Validate" |
Command2 | Name
Caption |
cmdExit
"Exit" |
After you modify the property settings, resize the controls and arrange them on the form until your user interface looks like the following:
The following shows the complete code for the form. To run the code as you read, select all the text, then copy it and paste it into the form of your own Microsoft® Visual Basic® project.
Option Explicit Private Sub cmdExit_Click() Unload Me End Sub Private Sub cmdValidate_Click() 'Clear the results window. txtResults = "" 'Create a SAX reader. Dim oReader As New Msxml2.SAXXMLReader50 'Create an XML schema cache. Dim oSC As New Msxml2.XMLSchemaCache50 'Create a class module for implementing content handler, 'the error handler, and the locator interfaces. Dim oValidator As New MyValidator 'Add the schema file to the schema cache. oSC.Add "", txtSchemaFile 'Configure the SAX reader. oReader.putFeature "schema-validation", True oReader.putProperty "schemas", oSC oReader.putProperty "schema-declaration-handler", oValidator Set oReader.contentHandler = oValidator Set oReader.errorHandler = oValidator 'Parse and validate the file. On Error Resume Next oReader.parseURL txtInputFile On Error GoTo 0 End Sub Private Sub Form_Load() txtInputFile.Text = App.Path + txtInputFile.Text txtSchemaFile.Text = App.Path + txtSchemaFile.Text End Sub
Validate Documents Using SAX | Overview of the SAX Validator Application | Sample XSD Schema File (SAX Validator) | MyValidator Class (SAX Validator) | Run the Application (SAX Validator) | How the SAX Validator Application Works