MSXML: testing for getNamedItem before use

Discussion in 'ASP General' started by DrewM, Oct 8, 2003.

  1. DrewM

    DrewM Guest

    I have an xml document fragment that I'm trying to process:

    <field id="summary" type="textarea" label="Summary" />
    <field id="content" type="wysiwyg" label="Content" />
    <field id="status" type="hidden" />

    (this is part of a bigger document loaded into XML DOM)

    I'm looping through the <field> elements and extracting their attributes
    into variables:

    <%
    for i = 0 to oFields.length-1
    nId = oFields(i).attributes.getNamedItem("id").text
    sType = oFields(i).attributes.getNamedItem("type").text
    sLabel = oFields(i).attributes.getNamedItem("label").text

    ...
    next
    %>

    The problem arises with the label attribute, because it is optional.
    Trying to retrieve the text of a non-existent attribute throws an error
    "Object required: 'oFields(...).attributes.getNamedItem(...)' ".

    How can I test for this so as to gracefully avoid the error?

    I've tried isNull() and isObject(), but these don't help. I'm beginning
    to think that the only way to do it is to throw an error and trap it.
    (Yuck).

    Any ideas?

    Thanks

    Drew
     
    DrewM, Oct 8, 2003
    #1
    1. Advertising

  2. DrewM

    Robert May Guest

    Try this (I'm assuming that this is VBScript):

    for i = 0 to oFields.length-1
    nId=GetAttributeValue(oFields(i),"id")
    sType=GetAttributeValue(oFields(i), "type")
    sLabel=GetAttributeValue(oFields(i), "label")

    ...
    next

    Function GetAttributeValue(byval p_sNode, byval p_sName)
    dim oNode ' this will be an XmlNode object
    dim sValue

    sValue=""

    set oNode=p_sNode.attributes.getNamedItem(p_sName)

    if Not oNode is Nothing then
    sValue=oNode.text
    End If

    GetAttributevalue=sValue

    End Function


    "DrewM" <> wrote in message
    news:e$...
    > I have an xml document fragment that I'm trying to process:
    >
    > <field id="summary" type="textarea" label="Summary" />
    > <field id="content" type="wysiwyg" label="Content" />
    > <field id="status" type="hidden" />
    >
    > (this is part of a bigger document loaded into XML DOM)
    >
    > I'm looping through the <field> elements and extracting their attributes
    > into variables:
    >
    > <%
    > for i = 0 to oFields.length-1
    > nId = oFields(i).attributes.getNamedItem("id").text
    > sType = oFields(i).attributes.getNamedItem("type").text
    > sLabel = oFields(i).attributes.getNamedItem("label").text
    >
    > ...
    > next
    > %>
    >
    > The problem arises with the label attribute, because it is optional.
    > Trying to retrieve the text of a non-existent attribute throws an error
    > "Object required: 'oFields(...).attributes.getNamedItem(...)' ".
    >
    > How can I test for this so as to gracefully avoid the error?
    >
    > I've tried isNull() and isObject(), but these don't help. I'm beginning
    > to think that the only way to do it is to throw an error and trap it.
    > (Yuck).
    >
    > Any ideas?
    >
    > Thanks
    >
    > Drew
    >
     
    Robert May, Oct 8, 2003
    #2
    1. Advertising

  3. DrewM

    DrewM Guest

    Robert May wrote:

    > Try this (I'm assuming that this is VBScript):
    >
    > for i = 0 to oFields.length-1
    > nId=GetAttributeValue(oFields(i),"id")
    > sType=GetAttributeValue(oFields(i), "type")
    > sLabel=GetAttributeValue(oFields(i), "label")
    > ...
    > next
    >
    > Function GetAttributeValue(byval p_sNode, byval p_sName)
    > dim oNode ' this will be an XmlNode object
    > dim sValue
    > sValue=""
    > set oNode=p_sNode.attributes.getNamedItem(p_sName)
    > if Not oNode is Nothing then
    > sValue=oNode.text
    > End If
    > GetAttributevalue=sValue
    > End Function


    Thanks, Robert. That was exactly what I needed.

    +1 beer.


    drew.
     
    DrewM, Oct 9, 2003
    #3
    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. KathyB
    Replies:
    0
    Views:
    1,017
    KathyB
    Jun 25, 2003
  2. William F. Robertson, Jr.
    Replies:
    1
    Views:
    975
    Kathy Burke
    Jun 25, 2003
  3. steve_h
    Replies:
    3
    Views:
    1,471
    Kirk Allen Evans [MVP]
    Nov 5, 2003
  4. Romeel
    Replies:
    1
    Views:
    946
    Fabien R
    Sep 18, 2003
  5. Vinay
    Replies:
    8
    Views:
    176
    Ben Morrow
    Jul 26, 2006
Loading...

Share This Page