Welcome to WebmasterWorld Guest from 50.16.78.128

Forum Moderators: httpwebwitch

Message Too Old, No Replies

How to check if an xml element exists?

Server side using ASP

   
2:36 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



Several of the xml docs I'm using can have formats where an xml element may or may not have a particular sub element. E.g.

<products>
<item>
<name>Yellow Widget</name>
<description>Small Yellow Widgets</description>
</item>
<item>
<name>Purple Widget</name>
</item>
</products>

In this example one "item" element has a "description" element, while another does not. If a "description" element is present I want to display it, and if not I want to display some text in its place.

Using Microsoft.XMLDOM I have code to display elements such as:

Set root_node=xmlObj.documentElement
Set products=root_node.selectNodes("item")

Response.Write products(counter).selectSingleNode("description").firstChild.nodeValue

The above line works fine if a "description" element exists, but produces an error message if it does not. Can anyone describe code that could be used to check whether an element does or does not exist? Thanks

3:28 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



TravelSite,
You have to remember, when using classic asp to parse xml, when you "set" a variable to hold a node, it then becomes an object.

So, when this happens, you can use the IsObject(Expression) function to test.

Example:
If isObject(products(counter).selectSingleNode("description")) Then
Response.Write products(counter).selectSingleNode("description").firstChild.nodeValue
Else
Response.Write("Does not exist.")
End If

5:12 pm on Sep 7, 2004 (gmt 0)

10+ Year Member



macrost,

Thanks for your reply, but for some reason the code provided always evaluates to true (my knowledge of asp is limited). E.g.

If isObject(products(counter).selectSingleNode("MadeUpName")) Then
Response.Write "test1"
Else
Response.Write "test2"
End If

On my server "test1" always appears.

....just found a solution that works on my server - researching the "isObject" function led me to it - so thanks :)

Set temp=products(counter-1).selectSingleNode("description")
If Not temp Is Nothing Then
Response.Write "aaaaaaaaa"
Else
Response.Write "bbbbbbbbbbbb"
End If

5:31 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The IsObject function tests the variable to see if it is an object pointer. Nothing is the reserved value for an empty/null object pointer. So, if the node evaluates to Nothing, IsObject will still return True. Instead, you need to compare it to Nothing. It looks like a double negative when you do it:


Dim objTest
Set objTest = products(counter).selectSingleNode("description").firstChild
If Not (objTest Is Nothing) Then
Response.Write objTest.nodeValue
End If

[edit]oops - I didn't see the last part of your last post. Never mind.[/edit]

1:05 am on Sep 8, 2004 (gmt 0)

10+ Year Member



Ahh yes, sorry about the half ass code there. john_k, thanks for fixing that for me.