SAX XML parsing in VBScript? 
Author Message
 SAX XML parsing in VBScript?

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.



Thu, 24 Nov 2005 06:23:33 GMT  
 SAX XML parsing in VBScript?

Quote:

> 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...

Bob Barrows



Thu, 24 Nov 2005 06:36:54 GMT  
 SAX XML parsing in VBScript?
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.

Quote:
>-----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

- Show quoted text -

Quote:

>Bob Barrows

>.



Thu, 24 Nov 2005 08:18:35 GMT  
 SAX XML parsing in VBScript?
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

Quote:

> 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

>> .



Thu, 24 Nov 2005 08:48:52 GMT  
 SAX XML parsing in VBScript?
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

>>> .

>.



Thu, 24 Nov 2005 09:22:10 GMT  
 SAX XML parsing in VBScript?
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?

Bob Barrows

Quote:

> 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.



Thu, 24 Nov 2005 22:38:43 GMT  
 SAX XML parsing in VBScript?
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

Quote:
> 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>

> >-----Original Message-----
> >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


> >> 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

> >>> .

> >.



Fri, 25 Nov 2005 16:38:48 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. SAX parsing XML in VB script

2. How to abort SAX xml parsing?

3. Abort SAX while is parsing xml file

4. parse xml file and vbscript

5. HELP - I need to parse xml with vbscript

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

7. SAX XML CSV converter using visual basic 6

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

9. use SAX in VBscript

10. About Sax in vbscript

11. Jscript for parsing ADO recordsets to XML

12. Parsing from XML & DTD to variables

 

 
Powered by phpBB® Forum Software