Application Form (SAX Validator)

 

[This sample application uses a feature that was first implemented in MSXML 4.0.]

To get started, create a new project and then reference Microsoft XML Core Services (MSXML).

To create a new project

  1. Open Microsoft® Visual Basic® 6.0. In the New Project dialog box, double-click Standard EXE.

To create a reference to MSXML

  1. On the Project menu, click References.

  2. In the Available References list, select Microsoft XML,v6.0, and then click OK.

    Note

    This example requires MSXM L4.0 or later and is written to show using MSXML 6.0.

Add Controls to the Form

Now build the user interface for the SAX validator application. Add the following controls to the form.

  • Three text boxes: one for entering the XML file input to be validated; one for entering the XSD file to use to validate the input file; and one for displaying search results.

  • Labels for each of the three text boxes

  • Two command buttons.

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:

Sample form

Add Code to the Form

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.SAXXMLReader60
  'Create an XML schema cache.
  Dim oSC As New Msxml2.XMLSchemaCache60
  '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 "urn:books", 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

See Also

Validate Documents Using SAX
Overview of the SAX Validator Application
Sample XML and XSD Files (SAX Validator)
MyValidator Class (SAX Validator)
Run the Application (SAX Validator)
How the SAX Validator Application Works