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>
Quote:
>-----Original Message-----
>In that case, I suggest you work on converting the VB
examples to vbscript.
Quote:
>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
>> 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.
>>> -----Original Message-----
>>>> 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.
>>> Check my dynamic listbox demo at
>>> http://www.thrasherwebdesign.com/index.asp?
>>> pi=links&hp=links.asp&c=&a=clear
>>> Bob Barrows
>>> .
>.