SAX parsing XML in VB script 
Author Message
 SAX parsing XML in VB script

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



Tue, 10 Feb 2004 02:27:38 GMT  
 SAX parsing XML in VB script

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



Tue, 10 Feb 2004 03:16:39 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. SAX XML parsing in VBScript?

2. How to abort SAX xml parsing?

3. Abort SAX while is parsing xml file

4. How to replace special characters in XML file other than using SAX

5. SAX XML CSV converter using visual basic 6

6. Parsing XML from an ASP page that Creates XML fails

7. vb.net newbie needs help with basic XML parsing

8. Reading/parsing an XML file into VB.

9. vb library/procedure for parsing html/xml/sgml like files

10. Parsing XML file in VB

11. Basic XML parse in VB COM question...

12. XML Parse in VB?

 

 
Powered by phpBB® Forum Software