Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - SAX2 Reference

Visual Basic Source: Mxvld.frm

Option Explicit

Private Function HandleErrors()
    MsgBox Err.Description
End Function

Private Sub Form_Resize()
    
    ' Resize text box to size of form.
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750

End Sub

Private Sub Form_Load()

    ' Resize text box to size of form.
    Text1 = ""
    Text1.Top = 100
    Text1.Left = 100
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height - 750
   
    'C reate standalone SAX validator.
    Dim vld As New MXValidator50
    Dim cnth As MSXML2.IVBSAXContentHandler
    Dim attrs As New SAXAttributes50

    ' Create the writer.
    Dim wrt As New MXXMLWriter50
    Set vld.errorHandler = wrt

    ' Create a schema collection to use in validation.
    Dim Sch As New XMLSchemaCache50

    ' Load schemas into the schema collection, Sch.
    Sch.Add "", App.Path & "\books.xsd"
    vld.putProperty "schemas", Sch
    Set cnth = vld

    On Error GoTo errorHandler

    ' Configure the writer to indent elements.
    wrt.indent = True

    ' Start the document.
    cnth.startDocument

    ' Add the XML declaration.
    cnth.processingInstruction "xml", "version='1.0'"

    'Add the <catalog> element to the page.
    cnth.startElement "", "catalog", "catalog", attrs

    ' Add the id attribute to the collection witht he "bk0101" value.
    attrs.addAttribute "", "", "id", "CDATA", "bk101"
    ' Create the <book id="bk101"> tag.
    cnth.startElement "", "book", "book", attrs
    ' Clear the attribute collection.
    attrs.Clear

    ' Create the <author>Gambardella, Matthew</author> string.
    cnth.startElement "", "author", "author", attrs
    cnth.characters "Gambardella, Matthew"
    cnth.endElement "", "author", "author"

    ' Create the <title>XML Developer's Guide</title> string.
    cnth.startElement "", "title", "title", attrs
    cnth.characters "XML Developer's Guide"
    cnth.endElement "", "title", "title"

    ' Create the <genre>Computer</genre> string.
    cnth.startElement "", "genre", "genre", attrs
    cnth.characters "Computer"
    cnth.endElement "", "genre", "genre"
    
    ' Create the <cost>44.95</cost> string.
    ' This element will be invalid according to books.xsd.
    cnth.startElement "", "cost", "cost", attrs
    cnth.characters "44.95"
    cnth.endElement "", "cost", "cost"

    ' Create the <publish_date>2000-10-01</publish_date> string.
    cnth.startElement "", "publish_date", "publish_date", attrs
    cnth.characters "2000-10-01"
    cnth.endElement "", "publish_date", "publish_date"

    ' Create the <description>An in-depth look at...</description> string.
    cnth.startElement "", "description", "description", attrs
    cnth.characters "An in-depth look at creating applications with XML"
    cnth.endElement "", "description", "description"

    ' Add closing tags for the <book> and <catalog> elements.
    cnth.endElement "", "book", "book"
    cnth.endElement "", "catalog", "catalog"

    ' End the document.
    cnth.endDocument

    Text1.Text = "Success!" & vbCrLf & vbCrLf & wrt.output
    Exit Sub
    

errorHandler:

    Text1.Text = "Failure:" & vbCr & vbLf & wrt.output

End Sub

Try It!

  1. Copy the XML schema definition file from Resource: books.xsd, and paste it into a text file. Save the file as books.xsd.
  2. Create a Standard EXE project in Visual Basic. Save the empty project as Mxvld.vbp to the same directory where you saved books.xsd. Name the form file Mxvld.frm.
  3. Create a reference to MSXML 5.0. To do this, select References... from the Project menu, and then check the box for Microsoft XML, v5.0.
  4. Double click on the TextBox icon from the tools menu. A TextBox control will appear on the project's form named Text1. In the properties for Text1, set the ScrollBars property to use "2 - Vertical" as the value and set the MultiLine property to True.
  5. Copy the Visual Basic code listing above, and paste it into the Visual Basic code editor to replace whatever code is already there.
  6. Execute the code by selecting Start from the Run menu.
  7. Verify that your output is similar to that listed in the Output topic. To view the contents of the output more easily, resize the application form as needed.