perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home »» Focus »» MSXML » SAX Sunday, 9 March 2008
 
NEWS
MSXML 4.0 SP2 now available!

 
MSXML
Basics
DOM
SAX
XPath
XSLT
Schemas
SOM
HTTP Access
.NET
Data Islands
Ask a Question
   Past Q&As
C++ Samples
DLL/Version Info
Reference Guide
Books
KB Articles
   HOW TO
   SAMPLE
   INFO
   BUG/PRB
   FIX
   Misc.
MSXML Tips
   August 2002
   September 2002
MSXML Tools

Microsoft XML Core Services

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.

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 Basic

Start 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

  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy