Overview of the SAX to DOM Example

MSXML 5.0 SDK

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

Overview of the SAX to DOM Example

This simple SAX to DOM example uses Microsoft® Visual Basic® code that creates DOMDocument and MXXMLWriter objects, sets the MXXMLWriter object as the ContentHandler, and then sets the output property of the MXXMLWriter object as the DOMDocument object. The ContentHandler method calls cause nodes to be built as the root document instance of the provided document. At the end of the procedure, DOM methods are used to return the name of the author, and to display the name in a message box.

To run the code sample

  1. Open Visual Basic 6.0, and in the New Project dialog box, double-click Standard EXE.
  2. In the Visual Basic 6.0 Window, from the Project menu, select References.
  3. Under Available References, select Microsoft XML, 5.0, and then click OK.
  4. In the Toolbox, click the Button control, and then add a button to the Form1 window.
  5. Add the following code to the Command1_Click procedure and then click Start on the toolbar.

Complete Code for the SAX to DOM Example

Private Sub Command1_Click()
    Dim xmlDoc As New Msxml2.DOMDocument50
    'If using SAX lexical handler, the following line is required.
    xmlDoc.validateOnParse = False

    Dim nodeList As IXMLDOMNodeList
    Dim wrt As New MXXMLWriter50
    Dim cnth As IVBSAXContentHandler
    'If using SAX lexical handler, the following line is required.
    Dim lexh As IVBSAXLexicalHandler
    Dim atrs As New SAXAttributes50
    Dim objNodeList

    Set cnth = wrt
    'If using SAX lexical handler, the following line is required.
    Set lexh = wrt
    wrt.output = xmlDoc

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

    'Starts the document.
    cnth.startDocument

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

    'Inserts DOCTYPE delcaration for DTD in DOM output.
    lexh.startDTD "catalog", "", "books.dtd"
    lexh.endDTD
    'You can remove or comment out previous two lines if 
    'you are not linking to a DTD.

    'Adds the <catalog> element to the page.
    cnth.startElement "", "", "catalog", atrs

    'Adds the id attribute to the collection witht he "bk0101" value.
    atrs.addAttribute "", "", "id", "CDATA", "bk101"
    'Creates the <book id="bk101"> tag.
    cnth.startElement "", "", "book", atrs
    'Clears the attribute collection.
    atrs.Clear

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

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

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

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

    'Ends the document.
    cnth.endDocument

    'Displays the author's name in a message box.
    Set objNodeList = xmlDoc.getElementsByTagName("author")
    MsgBox objNodeList.Item(0).Text
End Sub

See Also

Setting a DOMDocument Object as MXXMLWriter Output | MXXMLWriter CoClass | output Property | DOMDocument