XMLDOM / Conditional Check for Object fails

Discussion in 'ASP General' started by joe, May 31, 2005.

  1. joe

    joe Guest

    I am having problems checking for the value of an XMLDOM object .

    Lets say my XMLDOM object was successfully created as objXMLDoc, and that
    has several nodes on it. In the case of a VBScript loop like below:

    '-------------------
    For x = 1 To 10

    Set oItemPrice = objXMLDoc.selectSingleNode("//Item[x]/Price")

    '--- conditional stuff that fails here

    Next
    '-------------------

    some of the Items don't have a price, therefore the object oItemPrice will
    fail at some point. So I want to check for this failure, but everything I do
    gives me errors.
    Examples:

    1. If not oItemPrice Then
    2. If oItemPrice.lenght = 0 Then
    3. If oItemPrice = empty or oItemPrice = "" or isnull(oItemPrice) Then
    4. If not (oItemPrice) Then

    I really don't know what to do. Most errors are like:
    " Object doesn't support this property or method"

    Any help is appreciated.
     
    joe, May 31, 2005
    #1
    1. Advertising

  2. joe

    joe Guest

    With JScript one can just go:

    oItemPrice = xmlDoc.selectSingleNode("//Item["+x+"]/Price");

    if (oItemPrice) { ...do stuff }
    else {...do other stuff}

    but because my XMLDOM code must go inside VBScrip pages, I have to figure
    the other one out.
    I tried to mix both languages up and ended up with even more problems.


    > some of the Items don't have a price, therefore the object oItemPrice will
    > fail at some point. So I want to check for this failure, but everything I

    do
    > gives me errors.
    > Examples:
    >
    > 1. If not oItemPrice Then
    > 2. If oItemPrice.lenght = 0 Then
    > 3. If oItemPrice = empty or oItemPrice = "" or isnull(oItemPrice) Then
    > 4. If not (oItemPrice) Then
     
    joe, May 31, 2005
    #2
    1. Advertising

  3. joe

    Mark Schupp Guest

    try this:

    If Not IsDomTextNode( oItemPrice) Then
    ....

    'check if selectSingleNode returned a valid text node
    Function IsDomTextNode( ByRef objNode )

    Dim strTmp

    On Error Resume Next
    strTmp = objNode.text

    If Err.Number = 0 Then
    IsDomTextNode = True
    Else
    IsDomTextNode = False
    End If

    End Function

    You should also be able to use the nodeType property of the node to see if
    it is the type you expect. I can't remember why I used to above approach.

    --
    --Mark Schupp


    "joe" <> wrote in message
    news:NU1ne.9698$...
    >I am having problems checking for the value of an XMLDOM object .
    >
    > Lets say my XMLDOM object was successfully created as objXMLDoc, and that
    > has several nodes on it. In the case of a VBScript loop like below:
    >
    > '-------------------
    > For x = 1 To 10
    >
    > Set oItemPrice = objXMLDoc.selectSingleNode("//Item[x]/Price")
    >
    > '--- conditional stuff that fails here
    >
    > Next
    > '-------------------
    >
    > some of the Items don't have a price, therefore the object oItemPrice will
    > fail at some point. So I want to check for this failure, but everything I
    > do
    > gives me errors.
    > Examples:
    >
    > 1. If not oItemPrice Then
    > 2. If oItemPrice.lenght = 0 Then
    > 3. If oItemPrice = empty or oItemPrice = "" or isnull(oItemPrice) Then
    > 4. If not (oItemPrice) Then
    >
    > I really don't know what to do. Most errors are like:
    > " Object doesn't support this property or method"
    >
    > Any help is appreciated.
    >
    >
    >
    >
    >
    >
    >
    >
     
    Mark Schupp, May 31, 2005
    #3
  4. joe

    joe Guest

    Mark Schupp:

    Your function worked for me. Thank you!
     
    joe, May 31, 2005
    #4
  5. joe

    joe Guest

    JScript and VBScript

    Lately I've been surfing the MS knowledge pages to find XML related stuff,
    and I've noticed that (of the two primary scripting languages used with ASP)
    Jscript has way much more prominence that VBscript in the examples given.
    Acutally, most of what I found was Jscript, C++ and Visual Basic.

    Is MS pushing JScript as a preferred scripting language for their present
    and future technologies? Not that I care, I like JScript. Just trying to see
    where things are heading...
     
    joe, Jun 1, 2005
    #5
  6. joe

    McKirahan Guest

    Re: JScript and VBScript

    "joe" <> wrote in message
    news:57kne.17283$...
    > Lately I've been surfing the MS knowledge pages to find XML related stuff,
    > and I've noticed that (of the two primary scripting languages used with

    ASP)
    > Jscript has way much more prominence that VBscript in the examples given.
    > Acutally, most of what I found was Jscript, C++ and Visual Basic.
    >
    > Is MS pushing JScript as a preferred scripting language for their present
    > and future technologies? Not that I care, I like JScript. Just trying to

    see
    > where things are heading...
    >



    JScript (JavaScript, ECMAScript) are often used client-side as they are
    cross-browser compatible; whereas, VBScript requires an IE browser.
     
    McKirahan, Jun 1, 2005
    #6
  7. Re: JScript and VBScript

    joe wrote:
    > Lately I've been surfing the MS knowledge pages to find XML related
    > stuff, and I've noticed that (of the two primary scripting languages
    > used with ASP) Jscript has way much more prominence that VBscript in
    > the examples given. Acutally, most of what I found was Jscript, C++
    > and Visual Basic.
    >
    > Is MS pushing JScript as a preferred scripting language for their
    > present and future technologies? Not that I care, I like JScript.
    > Just trying to see where things are heading...


    IE is the only browser that will run vbscript in client-side code ... If you
    want cross-browser capabilities, you need to use javascript/jscript when
    writing client-side code.

    Bob Barrows

    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
     
    Bob Barrows [MVP], Jun 1, 2005
    #7
  8. joe

    David Patow Guest

    Sorry about coming late to this party, but I think there's a much simpler
    solution ...

    Set oItemPrice = objXMLDoc.selectSingleNode("//Item[x]/Price")
    If Not oItemPrice Is Nothing Then
    ' You can use oItemPrice in here.
    End If

    Yes, VB and VBScript are a little odd in this area, because they prefer to
    use the default property of an object when assigning and comparing. This is
    why there is the special "Set" statement and the above "Is" operator, which
    act upon the object pointer itself, not its default property.
     
    David Patow, Jun 9, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trip

    XMLHttpRequest + XMLDOM

    Trip, Oct 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    459
  2. Robert Zurer
    Replies:
    0
    Views:
    474
    Robert Zurer
    Jun 28, 2005
  3. Trip

    XMLHttpRequest + XMLDOM

    Trip, Oct 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    519
    intrader
    Oct 11, 2005
  4. Lamberti Fabrizio
    Replies:
    0
    Views:
    643
    Lamberti Fabrizio
    Jun 23, 2004
  5. Alec S.
    Replies:
    10
    Views:
    10,268
    Alec S.
    Apr 16, 2005
Loading...

Share This Page