將SAX加入我們的ASP應(yīng)用中
發(fā)表時間:2024-06-09 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]在處理大型的XML文檔的時候,在服務(wù)器端直接使用XML DOM的時候速度是比較慢的,當(dāng)我第一次接觸到SAX的時候,我意識到我們應(yīng)該在服務(wù)器端將DOM和SAX結(jié)合起來以提高我們程序的效率。我們在ASP中最常使用的就是使用COM來完成這個工作(也許你有更好的方法,下面只是包含了最基本的代碼而已 )。 ...
在處理大型的XML文檔的時候,在服務(wù)器端直接使用XML DOM的時候速度是比較慢的,當(dāng)我第一次接觸到SAX的時候,我意識到我們應(yīng)該在服務(wù)器端將DOM和SAX結(jié)合起來以提高我們程序的效率。我們在ASP中最常使用的就是使用COM來完成這個工作(也許你有更好的方法,下面只是包含了最基本的代碼而已 )。
首先我們創(chuàng)建一個DLL來封裝SAX的功能好了。
測試環(huán)境:Win2k Prof.+MSXML3.0 sp1+VB6
要使用SAX我們必須引用(Reference)Microsoft XML 3.0(我的機器安裝的是MSXML3.0 sp1)
工程名:SAXTesting
類名:clsSAXTest
方法:Public Function MyXMLParser(XML文件物理路徑) as DOMDocument
代碼:
Option Explicit
Public Function MyXMLParser(ByVal strXMLFilePath As Variant) As DOMDocument
Dim reader As New SAXXMLReader
Dim contentHandler As New ContentHandlerImpl
Dim errorHandler As New ErrorHandlerImpl
Set reader.contentHandler = contentHandler
Set reader.errorHandler = errorHandler
On Error GoTo ErrorHandle
Dim XMLFile As String
XMLFile = strXMLFilePath
reader.parseURL (XMLFile)
Dim xmlDoc As MSXML2.DOMDocument
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
xmlDoc.loadXML strXML
Set MyXMLParser = xmlDoc
Set xmlDoc = Nothing
Exit Function
ErrorHandle:
Err.Raise 9999, "My XML Parser", Err.Number & " : " & Err.Description
End Function
類名:modPublic
代碼:
Option Explicit
Global strXML As String
類名:ContentHandlerImpl
代碼:
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
intLocker = intLocker + 1
If intLocker > 1 Then
End If
strXML = strXML & "<" & strLocalName
For i = 0 To (attributes.length - 1)
strXML = strXML & " " & attributes.getLocalName(i) & "=""" & attributes.getValue(i) &
""""
Next
strXML = strXML & ">"
If strLocalName = "qu" Then
Err.Raise vbObjectError + 1, "ContentHandler.startElement", "Found element <qu>"
End If
End Sub
[page_break]Private Sub IVBSAXContentHandler_endElement(strNamespaceURI As String, strLocalName As String,
strQName As String)
strXML = strXML & "</" & strLocalName & ">"
End Sub
Private Sub IVBSAXContentHandler_characters(text As String)
text = Replace(text, vbLf, vbCrLf)
strXML = strXML & 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)
strXML = strXML & "<?" & 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
類名:ErrorHandlerImpl
代碼:
Option Explicit
Implements IVBSAXErrorHandler
Private Sub IVBSAXErrorHandler_fatalError(ByVal lctr As IVBSAXLocator, msg As String, ByVal
errCode As Long)
strXML = strXML & "*** error *** " & msg
End Sub
Private Sub IVBSAXErrorHandler_error(ByVal lctr As IVBSAXLocator, msg As String, ByVal errCode As
Long)
End Sub
Private Sub IVBSAXErrorHandler_ignorableWarning(ByVal oLocator As MSXML2.IVBSAXLocator,
strErrorMessage As String, ByVal nErrorCode As Long)
End Sub
OK,讓我們編譯這個DLL,應(yīng)該是沒什么問題了。讓我們在ASP中看看運行的結(jié)果怎么樣:
XML文件:
<?xml version="1.0"?>
<root foo="bar.com">
<PARTS>
<PART foo="bar.com" foo2="bar.com">
<PARTNO>12345</PARTNO>
<DESCRIPTION>VIP - Very Important Part</DESCRIPTION>
</PART>
<PART>
<PARTNO>5678</PARTNO>
<DESCRIPTION>LIP - Less Important Part</DESCRIPTION>
</PART>
</PARTS>
</root>
ASP文件:
<%
Set a = CreateObject("SAXTesting.clsSAXTest")
Set xmlDoc = a.MyXMLParser("D:\test.xml")
Response.contenttype="text/xml"
response.write xmlDoc.xml
set xmlDoc=nothing
set a=nothing
%>