Share via


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 3.0. To do this, select References... from the Project menu, and then check the box for Microsoft XML, v3.0.

    Note

    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.

  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 DOMDocument30
    'Create the SAX reader.
    Dim rdr As New SAXXMLReader30
    'Create the XML writer.
    Dim wrt As New MXXMLWriter30

    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 Method1
IXMLDOMDocument-DOMDocument
Sample XML File (books.xml)