I've attached a post from Mark Pryor from January of this year that might help...
--
Michael Harris
Microsoft.MVP.Scripting
--
Please do not email questions - post them to the newsgroup instead.
--
Quote:
> I 'm looking how I can pars an XML file with SAX. I know that you can
> parse an XML file using the DOM parser (using msxml3.0 of Microsoft).
> But how can I use SAX in VB script? Can anyone help me?
> Thanks,
> JeeBee
[ Attached Message ]
From: |
|
To: |
|
Date: |
Wed, 10 Jan 2001 12:52:09 -0800 |
Local: |
Wed, Jan 10 2001 3:52 pm
|
Subject: |
Re: Microsoft should provide SAX ActiveX component useable from JScript & VBScript!!! |
Quote:
>I just found out that we cannot use the SAX API provided in MSXML directly
>with scripting languages like VBScript and JScript. SAX API in MSXML can
>only be used from VC++ or VB.
>Since scripting languages provide for event handling, then Microsoft should
>provide an ActiveX component (that works in conjunction with MSXML old SAX
>interface) that can be used directly from the scripting languages (JScript
>and VBScript).
>Colbert Philippe
>Matrix Logic Consulting Inc
Huh? This works!
Regards,
Mark Pryor
' begin wshSAX.vbs
Option Explicit
'---------------------------------------------
' keywords=sax api VB5 xml
' description =test script for VBXMLSAX.dll SAX Wrapper
' author= Mark Pryor
' date = 1-10-01
' CustomObjectCodeBase=http://www.vbxml.com/xml/articles/vbxmlsax.asp
' DLLAuthor= Martin Naughton
' --------------------------------------------------
' main objects
dim oSxRdr, oSxCnt, oSxErr
set oSxRdr = WScript.CreateObject("VBXMLSAX.CVBSAXXMLReader30")
set oSxCnt = WScript.CreateObject("VBXMLSAX.CVBSAXContentHandler","oSxCnt_")
set oSxErr = WScript.CreateObject("VBXMLSAX.CVBSAXErrorHandler","oSxErr_")
'call back objects
'VBXMLSAX.CVBErrorInfo
'VBXMLSAX.CVBSAXLocator
dim oErrInfo, oLocator, oAttributes
set oErrInfo = WScript.CreateObject("VBXMLSAX.CVBErrorInfo")
set oLocator = WScript.CreateObject("VBXMLSAX.CVBSAXLocator")
set oAttributes = WScript.CreateObject("VBXMLSAX.CVBSAXAttributes")
' set implements
set oSxRdr.ContentHandler = oSxCnt
set oSxRdr.ErrorHandler = oSxErr
'
dim sFile, lResult, m_sOutput
sFile = "C:\WINDOWS\DESKTOP\test.xml"
lResult = parse( sFile )
msgbox "wait"
WScript.echo m_sOutPut
' clean up
set oAttributes = nothing
set oLocator = nothing
set oErrInfo = nothing
set oSxRdr = nothing
set oSxCnt = nothing
set oSxErr = nothing
WScript.quit
function parse( ByVal sPath)
' it won't work without the ByVal keyword
dim lResult
'call InitErrorInfo
With oSxRdr
.BaseURL = sPath
.SecureBaseURL = sPath
End With
WScript.echo sPath, IsObject( oSxRdr)
on error resume next
oSxRdr.ParseURL( sPath)
if err then
msgbox err.description
end if
parse = vbtrue
end function
Sub InitErrorInfo()
with oLocator
.LineNumber = 0
.ColumnNumber = 0
.PublicId = ""
.SystemId = ""
end with
End Sub
sub oSxCnt_Characters( chars , abort )
'WScript.echo "callback Cnt_Chararcters"
m_sOutput = m_sOutput & chars '& vbCrLf
end sub
sub oSxCnt_StartElement(NamespaceUri, LocalName , QName, oAttributes , Abort)
'WScript.echo "callback Cnt_StartElement"
dim m_lElementCount
If Not Abort Then
'Append to custom output
'm_sOutput = m_sOutput & "<" & LocalName '& vbCrLf
m_sOutput = m_sOutput & "<" & LocalName
'Increment count
m_lElementCount = m_lElementCount + 1
'Debug.Print LocalName
Dim lLength
lLength = oAttributes.GetLength
Dim lCounter 'As Long
Dim lUPointerLocalName 'As Long
Dim pcchLocalName 'As Long
Dim sQName 'As String
Dim sLocalName 'As String
Dim sValue 'As String
Dim sURI 'As String
Dim sName 'As String
Dim lType 'As SAX_ATTRIBUTE_TYPE
If lLength > 0 Then
For lCounter = 0 To lLength - 1
Call Attributes.GetLocalName(lCounter, sLocalName)
'm_sOutput = m_sOutput & " " & sLocalName & "="
m_objOutput = m_objOutput & " " & sLocalName & "="
Call Attributes.GetQName(lCounter, sQName)
Call Attributes.GetURI(lCounter, sURI)
lType = -1
lType = Attributes.GetTypeFromQName(sQName)
lType = -1
lType = Attributes.GetTypeFromName(sURI, sLocalName)
sValue = ""
Call Attributes.GetValueFromQName(sQName, sValue)
sValue = ""
Call Attributes.GetValueFromName(sURI, sLocalName, sValue)
Call Attributes.GetName(lCounter, sURI, sName, sQName)
lType = Attributes.GetType(lCounter)
Call Attributes.GetValue(lCounter, sValue)
' If sValue = "ToCity" Then
' Abort = True
' End If
'm_sOutput = m_sOutput & Chr(34) & sValue & Chr(34) '& vbCrLf
m_objOutput = m_objOutput & Chr(34) & sValue & Chr(34)
Next
End If
'm_sOutput = m_sOutput & ">" '& vbCrLf
m_sOutput = m_sOutput & ">"
Dim iFirst 'As Integer
Dim lCount 'As Long
If lLength > 0 Then
lCount = Attributes.GetType(0)
End If
End If
end sub
sub oSxCnt_ProcessingInstruction(Target, Data, Abort)
'WScript.echo "callback Cnt_ProcessingInstruction"
m_objOutput = m_objOutput & "</" & Target & ">"
end sub
sub oSxCnt_EndElement( NamespaceUri , LocalName , QName , Abort )
'WScript.echo "callback Cnt_EndElement"
m_sOutput = m_sOutput & "</" & LocalName & ">" '& vbCrLf
end sub
sub oSxErr_Error( oLocator , oErrorInfo )
WScript.echo "callback Err_StartElement"
end sub
' end script