Choosing SAX

Team Fly 

Page 476

Choosing SAX

DOM can be used right along with SAX, if you wish, but normally you select the set of tools appropriate to the job at hand. SAX is best for searching, particularly simple searches or when large documents are involved. DOM is best for tasks involving document modification, or when the task is complex (for instance, when you are dealing with internal cross-reference structures such as ID and IDREF).

DOM builds a verbose, navigable tree structure in memory—it even adds a type description for each node. If you're merely interested in reading through a large XML document, you normally wouldn't want to hand it over to DOM. But do remember that you can use the technologies together. You could emit a SAX stream from a DOM tree, or ask SAX to build a DOM tree.

Let's take a look at how SAX works first because it's the simpler of the two technologies.

Copying the Sample File

Before going further, you should now copy a sample XML file to your C:\ drive. Several examples in this chapter require this XML document.

As with the famous Pubs and Northwind sample databases, Microsoft offers a sample XML file for you to experiment with. Included with Visual Studio is a file in the Help system you should now save to your hard drive. Save it in a file named books.xml on your C:\ drive. Use the VB.NET Help Search feature (click the Search tab at the bottom of the Help window) and search for:

 <author>Gambardella, Matthew</author> 

You see an entry titled Sample HTML File For XML Data Islands. Copy it to Notepad. The data you want begins with the usual ?:

 <?xml version=''1.0"?> <catalog> 

and it ends like this:

    </book> </catalog> 

Using SAX

To see how to access a SAX stream in .NET, start a new VB.NET Windows-style project, then type in the code in Listing 17.1.

LISTING 17.1: ACCESSING A SAX STREAM

Imports System.Xml

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Xreader = New XmlTextReader("c:\books.xml")
        Dim ele, att As Integer, m As String

        While Xreader.Read()

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net