473,394 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

MSXML: testing for getNamedItem before use

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

Jul 19 '05 #1
2 5047
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" <bo***@doesntexist.com> wrote in message
news:e$*************@TK2MSFTNGP11.phx.gbl...
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

Jul 19 '05 #2
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.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jason Burr | last post by:
I keep getting a server error 400 - Bad Request with the code below. xmlDoc is the MSXML.DOMDocument populated with the xml data to submit. That data is well formed and works fine if I urlencode...
6
by: Alfred Taylor | last post by:
I'm having performance/memory problems using .NET's XslTransform class so I thought I'd give the MSXML object's a whirl. The question I haven't been able to find in these groups is can I use C#...
3
by: Daniel | last post by:
Are the differences between MSXML and .Net XSL transformation documented online anywhere? Many of my XSL's work in MSXML but transform differently in ..Net XSL transformation.
4
by: K | last post by:
I've an XML file in UTF-8. It contains some chinese characters ( both simplified chinese and traditional chinese). In loading the XML file with MSXML parser, I used the below code to retrieve...
0
by: MLH | last post by:
In the following code snippet, notice ("PIN=1234567890&MSSG=Here+is+a+short+message+-+no+spaces+-+just+plusses&Q1=0") I have it that way because I thought PLUSes were required and that spaces...
1
by: Michael Nemtsev | last post by:
Any kind of differences? Were they buid on the common base or different? -- WBR, Michael Nemtsev :: blog: http://spaces.msn.com/laflour "At times one remains faithful to a cause only...
3
by: Sharon | last post by:
How can I find what MSXML version I have installed? -- Thanks Sharon
13
by: yawnmoth | last post by:
<http://www.quirksmode.org/book/printable/xmlhttp.txtshows two alternatives to Microsoft.XMLHTTP - Msxml2.XMLHTTP and Msxml3.XMLHTTP. If my understanding is correct, the different numbers refer to...
4
by: mrjaxon | last post by:
I have a C# web application which leverages MSXML that I am trying to migrate to a 64 bit environment. Currently the application is built on the .NET 2.0 Framework and using MSXML 6 (though I had...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.