-
asp.net教程之将SAX加入我们的ASP应用中
在处理大型的XML文档的时候,在服务器端直接使用XML DOM的时候速度是比较慢的,当我第一次接触到SAX的时候,我意识到我们应该在服务器端将DOM和SAX结合起来以提高我们程序的效率。我们在ASP中最常使用的就是使用COM来完成这个工作(也许你有更好的方法)。
废话少说,还是直接进入实际的代码部分了(下面只是包含了最基本的代码而已)。
首先我们创建一个DLL来封装SAX的功能好了。
测试环境: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
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,应该是没什么问题了。让我们在ASP中看看运行的结果怎么样:
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
%>
废话少说,还是直接进入实际的代码部分了(下面只是包含了最基本的代码而已)。
首先我们创建一个DLL来封装SAX的功能好了。
测试环境: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
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,应该是没什么问题了。让我们在ASP中看看运行的结果怎么样:
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
%>
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式