SAX or Simple API for XML, is an alternative to DOM, and can be used to parse (and also create) XML documents. SAX is based on streaming model. The SAX parser reads input XML stream and generates various parsing events that an application can handle. With each parsing event, the parser sends sufficient information about the node being parsed. Unlike DOM, SAX does not build an in-memory representation of the source XML document, and hence it is an excellent alternative when parsing large XML documents, as SAX does not require that much memory (and resources). MSXML supports SAX 2.0.
Tip: Chapter 6 [Simple API for XML (SAX) in MSXML] in the book XML Application Development with MSXML 4.0 covers MSXML SAX features in great details. Dianne Arrow (Wrox Press) wrote an excellent review about this chapter.
- MSXML 3.0 SAX Documentation
- MSXML 3.0 SAX Reference
- MSXML 4.0 SAX Documentation
- MSXML 4.0 SAX Reference
Let's now look at how to use MSXML 4.0 SAX in Visual Basic.
The following XML document (c:\profits.xml) is used in the examples in this section. The SAX example scans this XML documents and print the name and profit amount of the zone with the highest profit.
<?xml version="1.0" encoding="UTF-8"?>
<Profits>
<East>20000</East>
<West>28000</West>
<North>13000</North>
<South>11000</South>
</Profits>
Visual BasicStart Visual Basic 6.0 and create a new Standard EXE project. Click on Project | Add Class Module and rename the class from Class1 to SAXContHandlerImpl. Click on Project | References and add a reference to Microsoft XML, v4.0 (MSXML4.DLL). Write following lines at the beginning of the class module:
Option Explicit
Option Explicit
Implements IVBSAXContentHandler
Public High_Amount As Double
Private CurAmount_Val As Double
Public HighProfit_Zone As String
Now, select IVBSAXContentHandler from the first combo box (General) followed by each method from the second combo box (Declarations). We'll leave all methods to have blank implementation, except following methods:
Private Sub IVBSAXContentHandler_startDocument()
High_Amount = -1
HighProfit_Zone = ""
End Sub
Private Sub IVBSAXContentHandler_characters(strChars As String)
On Error Resume Next
CurAmount_Val = CDbl(strChars)
On Error GoTo 0
End Sub
Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, strLocalName As String, strQName As String)
If CurAmount_Val > High_Amount Then
High_Amount = CurAmount_Val
HighProfit_Zone = strLocalName
End If
End Sub
Next, double click on the form and write following code in the Form_Load method:
Dim objReader As New MSXML2.SAXXMLReader40
Dim CHandler As New SAXContHandlerImpl
Set objReader.contentHandler = CHandler
objReader.parseURL "c:\profits.xml"
MsgBox CHandler.High_Amount
MsgBox CHandler.HighProfit_Zone
With the above XML document (c:\profits.xml), this application will messagebox 28000 and West.
MSXML SAX Resources Around the Web
- SAX, the Simple API for XML
- MSXML SAX KB Articles
-
MSXML 3.0 Illustrated
- Some Fun with SAX
- Add a SAX Parser to Your Apps
- No SAX please we’re British
- SAX 2.0 Programming With Visual Basic
- SAX and Microsoft XML Parser 3.0
- The Joy of SAX: a Visual Basic Sample
|