Application Form (Simple Filter)


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, and 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,v3.0, and then click OK.


    This example is written to show using MSXML 3.0, but you can use a later version, by setting your project reference accordingly here and updating any version-specific ProgIDs. For example, update the code so that any SAXXMLReader30 objects are of SAXXMLReader40 type instead if you are setting a reference to MSXML 4.0 in this step.

Add Controls to the Form

Now you must build the user interface for the simple filter application. Add the following controls to the form.

  • A text box, Text1, with the Multiline property set to True. This text box should be large enough to display the results of the parse process.

  • A text box, Text2, with "books.xml", the name of the file that the application parses and converts, specified as the Text property.

  • A text box, Text3. This text box is used to specify the element name that serves as the filter criteria.

  • A command button, Command1, with the caption "Parse". This button starts the parse process.

  • A command button, Command2, with the caption "Exit". This button closes the application.

The form should look like the following.

Sample form

Add Code to the Form

The code for the form:

  • Creates instances of MXXMLWriter and SAXAttributes objects.

  • Creates an instance of the SAXXMLReader.

  • Creates an instance of the ContentHandlerImpl class implementing the IVBSAXContentHandler and IVBSAXErrorHandler interfaces.

  • Sets the errorHandler and the contentHandler properties for the SAXXMLReader to point to the ContentHandlerImpl class.

  • Sets the instance of MXXMLWriter to variables in the ContentHandlerImpl class that hold references to the IVBSAXContentHandler and IVBSAXErrorHandler interfaces. This enables the MXXMLWriter object to receive ContentHandler and ErrorHandler events.

  • Calls the SetFilterCriteria method in the ContentHandlerImpl class and sets the filter criteria.

  • Starts the parsing operation.

Complete Code for the Form

'Create a writer and an attribute helper.
Public oWriter As New MXXMLWriter30
Public atrs As New SAXAttributes30

Private Sub Command1_Click()
'Create the reader.
Dim rdr As New SAXXMLReader30
'Create the content handler.
Dim cnth As New ContentHandlerImpl
'Set the content handler for the reader.
Set rdr.contentHandler = cnth
'Set the error handler for the reader.
Set rdr.errorHandler = cnth
'Set the writer for the content handler.
Set cnth.oContentHandler = oWriter
'Set the error handler for the writer.
Set cnth.oErrorHandler = oWriter

'Configure output for the writer.
oWriter.indent = True
oWriter.standalone = True
oWriter.output = ""
oWriter.omitXMLDeclaration = True

'Set the filter criteria in the content handler.
cnth.SetFilterCriteria (Text3.Text)

On Error GoTo HandleError
'Parse the document.
rdr.parseURL (Text2.Text)

Text1.Text = oWriter.output

Exit Sub
    If Not cnth.errorHappen Then
        Text1.Text = "**** Error **** " & Err.Number & " : " & Err.Description
    End If
End Sub
Private Sub Command2_Click()
    Unload Me
End Sub

See Also

How Filter Criteria is Set
IMXWriter Interface
MXXMLWriter CoClass
ISAXContentHandler Interface
ISAXErrorHandler Interface