|
From: Hogan L. <ho...@Ne...> - 2001-06-10 23:11:33
|
Ok, I seem to remember a while back people we complaining
about the speed that it took at load up if you had a lot of
expansion files. (This will always be a trade-off with xml because
by its nature xml is not as fast as more traditional dbs)
However, I was recently playing around with the new release of
the microsoft interface and I noticed that the SAX2 standard has
been implemented. For those of you that don't know SAX is a
"event" driven parsing system for xml. This type of parsing is
really good if you only want a little information out of a large file
(as the following example shows). What it does is "call-back"
procedures specified in the interface (Java developers will
instantly grasp this) when events happen. In this case I only
care about the start even for the expansion tag. I look at the
attributes and store them and then abort the rest of the
processing. The source code for this part is small only about
10 lines, but it is blazingly fast. This is because unlike with
the DOM the parser only has to do one read and parse 3 lines
from the file before I abort parsing. It also does not need to
store any in memory information (ie the tree that the DOM stores)
Clearly SAX could be (and should) be used to scan the card names
and ignore other elements of and expansion file - thus speeding
up the startup time on a system that needs all the card names in
memory.
This example also stores all the names (and other information)
in a collection - thus there are two classes - the collection of
expansions (ExList.cls) and a single expansion's information
(ExDescrip). This example is written in VisualBasic (hey! it is
what I've got) but it should be easy to modify for Java.
Please let me know if you have any comments or questions -
I hope this example helps you to streamline your code and see
how powerful the SAX implementation can be. (There are also
SAX implementations in Java, perl etc etc.
ho...@Ne...
Hogan
----------------------------------------begin file ExList.cls
'local variable to hold collection
Private mCol As Collection
Public Sub setup(path As String)
'myRootDir = path
Dim fileName As String
Dim index As Integer
index = 1
fileName = Dir(path + "\ex.*.xml", vbNormal)
Do While fileName <> ""
Dim newItem As ExDescrip
Set newItem = New ExDescrip
newItem.setup (path + "\" + fileName)
mCol.Add Item:=newItem, Key:=CStr(index)
index = index + 1
fileName = Dir
Loop
End Sub
Public Property Get Item(vntIndexKey As Variant) As ExDescrip
Attribute Item.VB_UserMemId = 0
Set Item = mCol(vntIndexKey)
End Property
Public Property Get Count() As Long
Count = mCol.Count
End Property
Public Sub Remove(vntIndexKey As Variant)
mCol.Remove vntIndexKey
End Sub
Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = mCol.[_NewEnum]
End Property
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub
----------------------------------------end file ExList.cls
----------------------------------------begin file ExDescrip.cls
Option Explicit
Implements IVBSAXContentHandler
'local variable(s) to hold property value(s)
Private myPath As String
Private myName As String
Private myNM As String
Private myVer As String
Public Property Get path() As String
path = myPath
End Property
Public Property Get name() As String
name = myName
End Property
Public Property Get nm() As String
nm = myNM
End Property
Public Property Get ver() As String
ver = myVer
End Property
Public Sub setup(path As String)
Dim read As New SAXXMLReader
Set read.contentHandler = Me
On Error GoTo 10 ' only going to read the start
read.parseURL path
10 myPath = path
End Sub
Private Sub IVBSAXContentHandler_startElement(strNamespaceURI As String, strLocalName As String, strQName As String, ByVal oAttributes As MSXML2.IVBSAXAttributes)
If strLocalName = "Expansion" Then
myName = oAttributes.getValueFromQName("name")
myNM = oAttributes.getValueFromQName("nmemonic")
myVer = oAttributes.getValueFromQName("version")
Err.Raise vbObjectError + 1, "ContentHandler.startElement", "Got all data, skip the rest of document"
End If
End Sub
Private Sub IVBSAXContentHandler_characters(strChars As String)
End Sub
Private Property Set IVBSAXContentHandler_documentLocator(ByVal RHS As MSXML2.IVBSAXLocator)
End Property
Private Sub IVBSAXContentHandler_endDocument()
End Sub
Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, strLocalName As String, strQName As String)
End Sub
Private Sub IVBSAXContentHandler_endPrefixMapping(strPrefix As String)
End Sub
Private Sub IVBSAXContentHandler_ignorableWhitespace(strChars As String)
End Sub
Private Sub IVBSAXContentHandler_processingInstruction(strTarget As String, strData As String)
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
----------------------------------------end file ExDescrip.cls
|