SAX XML parsing in VBScript?

658 views
Skip to first unread message

Tom Stone

unread,
Jun 7, 2003, 6:23:33 PM6/7/03
to
Does anyone have any examples of parsing an XML document
using VBScript (or JavaScript?)? Just a simple example to
get me started would be great -- like one that takes an
XML file, returns the values from elements with a
particular name. All the examples in the microsoft
documentation are in VB or C++, and I want to get a simple
one that will work as part of a webpage.

Thanks,

Tom S.

Bob Barrows

unread,
Jun 7, 2003, 6:36:54 PM6/7/03
to

Tom Stone

unread,
Jun 7, 2003, 8:18:35 PM6/7/03
to
Sorry... I should have stressed (as my subject line
indicated) that I need this to be using SAX, not DOM. Your
listbox example looks like it uses DOM. I want to try
using SAX with VBScript or JScript in a web page on the
client (IE).

Thanks,

Tom S.

>.
>

Bob Barrows

unread,
Jun 7, 2003, 8:48:52 PM6/7/03
to
In that case, I suggest you work on converting the VB examples to vbscript.
It's not too difficult. The main differences are:

1. in vbscript, all variables are variant: remove all instances of "As
datatype" in both variable and function declarations
2. object creation syntax: objects in asp/vbscript must be instantiated
using server.createobject, so anywhere you see "dim x as New y" or "set x =
new y" change it to 'set x = server.createobject("y")'

If you have any compile errors after doing this, show us and we'll be able
to help you out.

Bob Barrows

Tom Stone

unread,
Jun 7, 2003, 9:22:10 PM6/7/03
to
Hi Bob,

Thanks for the time... I have been doing what you
suggested: trying to convert the VB SAX Filtering example
from the MSXML SDK into a VBScript version -- and I
already knew to replace typed variables with variants and
so on.

The problem is, I think I need to create a VBScript Class
to use the SAX implementation, and I have no experience
with VBScript classes. Also, I don't know that this is
possible... inside my class I can't seem to be able to use
the Implements keyword, which would seem to be critical
here. Also, it won't let me set object variables in the
class, or maybe I am doing it wrong. I have commented out
some of the stuff in the Class below... before the methods
and properties are defined, because they are giving me
erors.

I think my initial code in the button OnClick event is
pretty good... after a lot of cleanup! But then the stuff
in the Class isn't working... I'm sure there is a lot to
fix in there, but I can't even get past the Implements and
the initial object variables.

Maybe this all isn't possible? In the SDK they only talk
about the SAX implementation for VB and C++ -- there is no
mention of VBScript or JScript... sigh.

Any ideas?

Thanks,

Tom S.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<html>
<head>
<title>Simple SAX Parser Example</title>
<link rel="stylesheet" type="text/css" href="mystyles.css">
<script language="VBScript">
<!--

Set oWriter = CreateObject("MSXML2.MXXMLWriter.4.0")
Set atrs = CreateObject("MSXML2.SAXAttributes.4.0")

Sub Button1_OnClick

'Create the reader.
Set rdr = CreateObject("MSXML2.SAXXMLReader.4.0")

'Create the content handler. THIS IS MY NEW CLASS... SEE
BELOW
Set cnth = New ContentHandlerImpl

'Set the content handler for the reader.
Set rdr.contentHandler = cnth

'Set the error handler for the reader.
Set rdr.errorHandler = cnth

'Set the writer for the content handler.
Set cnth.oContentHandler = oWriter

'Set the error handler for the writer.
Set cnth.oErrorHandler = oWriter

'Configure output for the writer.
oWriter.indent = True
oWriter.standalone = True
oWriter.output = ""
oWriter.omitXMLDeclaration = True

'Set the filter criteria in the content handler.
'cnth.SetFilterCriteria (ElementName.Value)


On Error Resume Next
'Parse the document.
rdr.parseURL (ParseMe.XMLFile.Value)

div1.innerHtml = oWriter.output

'div1.InnerHtml = ParseMe.XMLFile.Value

If Not cnth.errorHappen Then
div2.InnerHtml = "**** Error **** at " & " is Error #"
& Err.Number & " : " & Err.Description
End If

Exit Sub

End Sub

Class ContentHandlerImpl

'Implements IVBSAXContentHandler
'Implements IVBSAXErrorHandler

'Declare a variable for setting the writer to the
content handler.
'Public oContentHandler As IVBSAXContentHandler

'Declare a variable for setting the writer to the
error handler.
'Public oErrorHandler As IVBSAXErrorHandler

'Flag to indicate if the error handler has thrown
a fatal error.
Public errorHappen

'Flag to indicate if the element is in scope.
Dim FilterTrue

'Declare a string to hold the element name.
Dim FilterCriteria

Private Sub IVBSAXContentHandler_characters(strChars)
Set oContentHandler = CreateObject
("MSXML2.IVBSAXContentHandler")
If FilterTrue Then
oContentHandler.characters strChars
End If
End Sub

Private Property Set IVBSAXContentHandler_documentLocator
(ByVal RHS)
Initialize
End Property

Private Sub IVBSAXContentHandler_endDocument()

End Sub

Private Sub IVBSAXContentHandler_endElement
(strNamespaceURI,strLocalName,strQName)
If FilterTrue Then
oContentHandler.endElement strNamespaceURI,
strLocalName, strQName
End If
If strLocalName = FilterCriteria Then
FilterTrue = False
End If
End Sub

Private Sub IVBSAXContentHandler_endPrefixMapping
(strPrefix)

End Sub

Private Sub IVBSAXContentHandler_ignorableWhitespace
(strChars)

End Sub

Private Sub IVBSAXContentHandler_processingInstruction
(strTarget,strData)

End Sub

Private Sub IVBSAXContentHandler_skippedEntity(strName)

End Sub

Private Sub IVBSAXContentHandler_startDocument()

End Sub

Private Sub IVBSAXContentHandler_startElement
(strNamespaceURI,strLocalName,strQName,ByVal oAttributes)
If strLocalName = FilterCriteria Then
FilterTrue = True
End If
If FilterTrue Then
oContentHandler.startElement strNamespaceURI,
strLocalName, _
strQName, oAttributes
End If
End Sub

Private Sub IVBSAXContentHandler_startPrefixMapping
(strPrefix,strURI)

End Sub

Private Sub Initialize()
errorHappen = False
FilterTrue = False
End Sub

Private Sub IVBSAXErrorHandler_error(ByVal
oLocator,strErrorMessage,ByVal nErrorCode)

End Sub

Private Sub IVBSAXErrorHandler_fatalError(ByVal
oLocator,strErrorMessage,ByVal nErrorCode)
Form1.Text1.Text = strErrorMessage & nErrorCode
errorHappen = True
End Sub

Private Sub IVBSAXErrorHandler_ignorableWarning(ByVal
oLocator,strErrorMessageString,ByVal nErrorCode)
End Sub

Public Sub SetFilterCriteria(elementname)
FilterCriteria = elementname
End Sub

End Class

-->
</script>
</head>

<body>

<table border="0" cellpadding="5" cellspacing="0"
width="700">
<tr>
<td colspan="3">
<h2>Simple SAX Parser</h2>
<p></p>
</td>
</tr>
<form name="ParseMe">
<tr>
<td>XML File: <input type="text" name="XMLFile"
size="25" tabindex="1"></td>
</tr>
<tr>
<td>Element: <input type="text" name="ElementName"
size="25" tabindex="2"></td>
</tr>
<tr>
<td><input type="button" name="Button1" tabindex="3"
value="Parse with SAX"></td>
</tr>
</form>
</table>

<br><br>

<div id="div1">Original Text in Div1</div>
<br><br>
<div id="div2">Original Text in Div2</div>

</body>
</html>

>.
>

Bob Barrows

unread,
Jun 8, 2003, 10:38:43 AM6/8/03
to
I really don't know: I've never used SAX. The DOM methods have always proven
adequate for my needs.

I'm wondering why you need a custom class to implement SAX - I had a brief
look at SAX at one point and never saw this requirement. I've got some
written material at work, so I'll take a look at it tomorrow. In the
meantime, here are some results from a Google search:

http://tinyurl.com/drls

So, most of these people are saying it can't be used in vbscript. So,
unfortunately it looks as though I've wasted your time. Sorry about that.

Could you explain why it needs to be SAX?

Joe Fawcett

unread,
Jun 9, 2003, 4:38:48 AM6/9/03
to
I don't think you can do it entirely in script because you can't return the
correct interfaces from a VBScript class. You'd have to use a COM class that
returned the required interface and call that from VBScript.

--

Joe
"Tom Stone" <tst...@rochester.rr.com> wrote in message
news:030d01c32d5c$62806a50$a401...@phx.gbl...

muslyy...@gmail.com

unread,
Jan 15, 2020, 11:37:46 AM1/15/20
to

muslyy...@gmail.com

unread,
Jan 15, 2020, 12:21:10 PM1/15/20
to
On Sunday, 8 June 2003 03:53:33 UTC+5:30, Tom Stone wrote:

muslyy...@gmail.com

unread,
Jan 15, 2020, 12:21:45 PM1/15/20
to
On Sunday, 8 June 2003 03:53:33 UTC+5:30, Tom Stone wrote:

muslyy...@gmail.com

unread,
Jan 15, 2020, 12:23:50 PM1/15/20
to
On Sunday, 8 June 2003 03:53:33 UTC+5:30, Tom Stone wrote:
Reply all
Reply to author
Forward
0 new messages