homepage Welcome to WebmasterWorld Guest from 54.242.126.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
How to check if an xml element exists?
Server side using ASP
TravelSite

10+ Year Member



 
Msg#: 34 posted 2:36 pm on Sep 7, 2004 (gmt 0)

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

 

macrost

10+ Year Member



 
Msg#: 34 posted 3:28 pm on Sep 7, 2004 (gmt 0)

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

TravelSite

10+ Year Member



 
Msg#: 34 posted 5:12 pm on Sep 7, 2004 (gmt 0)

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

john_k

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 34 posted 5:31 pm on Sep 7, 2004 (gmt 0)

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]

macrost

10+ Year Member



 
Msg#: 34 posted 1:05 am on Sep 8, 2004 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved