Share via

Connecting an MXXMLWriter to a SAXXMLReader


In many of the applications that you create with the latest version of the Simple API for XML (SAX2), you use both SAXXMLReader, which reads an XML document and passes events, and MXXMLWriter, which catches the events and produces output based on the data passed by the events. Before MXXMLWriter can catch events, however, you must connect it to SAXXMLReader. MXXMLWriter, on its own, does not provide the handlers required to catch events thrown by SAXXMLReader.

To enable MXXMLWriter to handle events, you must assign the SAX2 handler interfaces to it. These handler interfaces include IVBSAXContentHandler, IVBSAXDTDHandler, IVBSAXDeclHandler, IVBSAXErrorHandler, and IVBSAXLexicalHandler. By assigning the SAX2 handler interfaces to your MXXMLWriter object, you make all the properties and methods of the interfaces available to it. In this way, your MXXMLWriter object can catch any of the events passed by SAXXMLReader. The following sections describe how to create SAXXMLReader and MXXMLReader objects and how to connect them.

Creating the SAXXMLReader and MXXMLWriter Objects

To create instances of SAXXMLReader and MXXMLWriter, use the following syntax. For the sample MXXMLWriter application, the creation of the SAXXMLReader and MXXMLWriter objects is in the Declarations section of the form.

Dim rdr As New SAXXMLReader30
Dim wrt As New MXXMLWriter30

Type Casting MXXMLWriter to Handler Interfaces

SAX2 provides several handler interfaces that are designed to catch events passed by SAXXMLReader. You set these handler interfaces as properties of SAXXMLReader. To make these handler interfaces available to MXXMLWriter, you type cast MXXMLWriter to them.

For example, the sample MXXMLWriter application sets the contentHandler property of the rdr (SAXXMLReader) object to the wrt (MXXMLWriter) object, as shown in the following code. This registers the wrt object as the ContentHandler of the rdr object. This also sets the dtdHandler and errorHandler properties. You must set the declaration-handler and lexical-handler properties through the putProperty method.

Private Sub Form_Load()
    Set rdr.contentHandler = wrt
    Set rdr.dtdHandler = wrt
    Set rdr.errorHandler = wrt
    rdr.putProperty "", _       wrt
    rdr.putProperty "", wrt
End Sub

Setting the Output Destination

In the sample MXXMLWriter application, clicking the Try File button clears both the text box and the output buffer (wrt.output). Then, the application calls the parse.URL method of SAXXMLReader and passes the name of the file in the TextFileName text box (as shown in bold in the following code sample). At this point, SAXXMLReader fires a series of events to pass the data in the XML document to the wrt object, which stores this data in the output buffer. By default, the output is a string value that, in this case, is set to the textResult.text box (as shown in bold in the following code sample). The output destination can also be set as an IStream.

Private Sub CommandTryFile_Click()
    ' Set parameters, clean the scene.
    TextSource.Text = ""
    wrt.output = ""
    wrt.omitXMLDeclaration = True
    On Error GoTo uhoh
    rdr.parseURL TextFileName.Text
    TextResult.Text = wrt.output
    Exit Sub
    TextResult.Text = "Error: " & Err.LastDllError & " -- " & _      Err.Description & vbCrLf & "Check if file exists in the _      correct place"
End Sub

See Also

Configuring MXXMLWriter Output
ISAXXMLReader Interface
IMXWriter Interface
ISAXContentHandler Interface
ISAXDeclHandler Interface
ISAXDTDHandler Interface
ISAXErrorHandler Interface
ISAXLexicalHandler Interface
output Property