Overview of the DOM to SAX Example

MSXML 5.0 SDK

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

Overview of the DOM to SAX Example

The DOM to SAX example is Microsoft® Visual Basic® code that executes when you click a command button on the application form. The code in this example performs the following tasks:

  • Creates DOMDocument, SAXXMLReader, and MXXMLWriter objects.
  • Sets the XML writer (wrt) object for the value of the ContentHandler property of the SAX reader (rdr) object.
  • Passes the DOM document (xmlDoc) object to the parse method of the SAX reader.
  • Writes the results of the SAX events to the output property of the SAX writer.
  • Displays the SAX writer output using a Visual Basic text box control on the application form.

To run the code sample

  1. Copy the sample XML file (books.xml) and paste it into a text file. Save the file as books.xml.
  2. Create a Standard EXE project in Visual Basic. Save the empty project as dom2sax.vbp to the same directory where you saved books.xml. Name the form file using the default name of Form1.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 a value of "2 - Vertical" and set the MultiLine property to True.
  5. From the View menu, click Code.
  6. Copy the Visual Basic code listing above, and paste it into the Visual Basic code editor, replacing any code already there.
  7. From the Run menu, click Start.
  8. Verify that the output is the same (pass-through) as the contents of the original books.xml. To view the contents of the output more easily, resize the application form as needed.

Complete Code for the DOM to SAX Example

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 = "<?xml version=""1.0""?>" & vbCrLf
    Text1.Top = 100
    Text1.Left = 100
    Text1.Width = Form1.Width - 350
    Text1.Height = Form1.Height – 750
    'Create a DOMDocument object.
    Dim xmlDoc As New DOMDocument50
    'Create the SAX reader.
    Dim rdr As New SAXXMLReader50
    'Create the XML writer.
    Dim wrt As New MXXMLWriter50

    On Error GoTo errorHandler

    'Load the DOM document.
    xmlDoc.async = False
    xmlDoc.Load  App.Path & "\books.xml"

    'Set properties on the XML writer.
    wrt.byteOrderMark = True
    wrt.omitXMLDeclaration = True
    wrt.indent = True

    'Set the XML writer to the SAX content handler.
    Set rdr.contentHandler = wrt
    Set rdr.dtdHandler = wrt
    Set rdr.errorHandler = wrt
    rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
    rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt

    'Parse the DOMDocument object.
    rdr.parse xmlDoc

    'Show the results in the text box.
    Text1.Text = Text1.Text & wrt.output

    Exit Sub

errorHandler:
    HandleErrors
End Sub

See Also

Passing a DOMDocument Object as a Parse() Parameter | ISAXXMLReader Interface | parse Method | DOMDocument | Sample XML File (books.xml)