For the JumpStart application, you create the ContentHandler
by adding a class that implements the IVBSAXContentHandler
interface. Although creating a class may sound complex, Microsoft® Visual Basic® automates most of the process.
Before you can create a class based on the IVBSAXContentHandler
interface, you must create a new project and then reference that project to Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office.
To create a new project
You must now instruct your application to use the MSXML2 type library provided with MSXML 5.0.
To create a reference to MSXML 5.0
You are now ready to create and implement the new ContentHandler
class.
To create a new class
To implement the ContentHandlerImpl Class
A drop down list appears with the all the available classes. (This list includes interfaces that are actually abstract classes.)
ContentHandlerImpl
class.This automatically adds code to the Class Window.
IVBSAXContentHandler
method or property that you want to add to this class. (You have to select each method even though you do not need to add code to each one.)
For example, select startElement
to add this method to the ContentHandlerImpl
class. (When parsing an XML document, the reader will invoke this method each time it encounters a new element in the document.) After you select startElement
from the list, the following code appears in the Class Window:
Private Sub IVBSAXContentHandler_startElement _ (strNamespaceURI As String, _ strLocalName As String, _ strQName As String, _ ByVal oAttributes As MSXML2.IVBSAXAttributes) End Sub
For the startElement
method added in Step 4, enter the code required to make the method look like this:
Private Sub IVBSAXContentHandler_startElement _ (strNamespaceURI As String, _ strLocalName As String, _ strQName As String, _ ByVal attributes As MSXML2.IVBSAXAttributes) Dim i As Integer Form1.Text2.text = Form1.Text2.text & "<" & strLocalName For i = 0 To (attributes.length - 1) Form1.Text2.text = Form1.Text2.text & " " _ & attributes.getLocalName(i) & "=""" _ & attributes.getValue(i) & """" Next Form1.Text2.text = Form1.Text2.text & ">" If strLocalName = "qu" Then Err.Raise vbObjectError + 1, "ContentHandler.startElement", _ "Found element <qu>" End If End Sub
ContentHandlerImpl
class.The preceding instructions explain how to create a new project with a reference to Microsoft XML, v5.0, and how to create a ContentHandler
class called ContentHandlerImpl
by implementing the IVBSAXContentHandler
interface. The following shows the complete code for the ContentHandlerImpl
class.
Option Explicit Implements IVBSAXContentHandler Private Sub IVBSAXContentHandler_startElement _ (strNamespaceURI As String, _ strLocalName As String, _ strQName As String, _ ByVal attributes As MSXML2.IVBSAXAttributes) Dim i As Integer Form1.Text2.text = Form1.Text2.text & "<" & strLocalName For i = 0 To (attributes.length - 1) Form1.Text2.text = Form1.Text2.text & " " & _ attributes.getLocalName(i) & "=""" & _ attributes.getValue(i) & """" Next Form1.Text2.text = Form1.Text2.text & ">" If strLocalName = "qu" Then Err.Raise vbObjectError + 1, "ContentHandler.startElement", _ "Found element <qu>" End If End Sub Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, _ strLocalName As String, _ strQName As String) Form1.Text2.text = Form1.Text2.text & "</" & strLocalName & ">" End Sub Private Sub IVBSAXContentHandler_characters(text As String) text = Replace(text, vbLf, vbCrLf) Form1.Text2.text = Form1.Text2.text & text End Sub Private Property Set IVBSAXContentHandler_documentLocator(ByVal RHS As _ MSXML2.IVBSAXLocator) End Property Private Sub IVBSAXContentHandler_endDocument() End Sub Private Sub IVBSAXContentHandler_endPrefixMapping(strPrefix As String) End Sub Private Sub IVBSAXContentHandler_ignorableWhitespace(strChars As String) End Sub Private Sub IVBSAXContentHandler_processingInstruction(target As String, _ data As String) Form1.Text2.text = Form1.Text2.text & "<?" & target & " " _ & data & ">" End Sub Private Sub IVBSAXContentHandler_skippedEntity(strName As String) End Sub Private Sub IVBSAXContentHandler_startDocument() End Sub Private Sub IVBSAXContentHandler_startPrefixMapping(strPrefix As String, _ strURI As String) End Sub
As you can see from the sample code, not all methods or properties have custom code added to them. When you implement a handler for the Simple API for XML (SAX2), you can add only the methods or properties that you need.
In the next section, you create the ErrorHandler
for the JumpStart application.