Forum Moderators: open
<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
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
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
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]