Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > MSXML: testing for getNamedItem before use

Reply
Thread Tools

MSXML: testing for getNamedItem before use

 
 
DrewM
Guest
Posts: n/a
 
      10-08-2003
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

 
Reply With Quote
 
 
 
 
Robert May
Guest
Posts: n/a
 
      10-08-2003
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" <(E-Mail Removed)> wrote in message
news:e$(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
 
DrewM
Guest
Posts: n/a
 
      10-09-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: best way of testing a program exists before using it? Tim Golden Python 3 09-12-2006 05:50 PM
RE: best way of testing a program exists before using it? Tim Golden Python 2 09-12-2006 03:03 PM
best way of testing a program exists before using it? Hari Sekhon Python 4 09-11-2006 07:13 PM
Before Sunset & Before Sunrise matt r DVD Video 2 11-25-2004 08:37 AM
testing--news2004--testing Boomer Computer Support 3 09-24-2003 06:54 PM



Advertisments