I've really been struggling to find a solution to this. My code is different from what I've seen on a Google search, and nothing I've tried will work. Basically as the title suggests I want to check if an Xml Node exists. This is because of erratic XML returned from a YouTube feed. Code is below.
<cfset YouTubeXml = xmlParse(#YouTubeFavourites.FileContent#) />
<cfset group = XmlSearch(YouTubeXml, "//media:group") />
<cfloop from="1" to="25" index="i">
<cfoutput>
#group[i]['media:thumbnail'][i]['url']#
</cfoutput>
</cfloop>
Basically the error says that #group[i]['media:thumbnail'][i]['url']#
doesn't exist. Hope someone can help.
The below code will check to ensure the variable exists before it tries to display it. The nested approach is the only way to ensure each piece exists as you go along. There are functions to make it look prettier, but that's a topic for another question.
I find it odd that you're using
i
twice in the variable name, but I'm not familiar with the Youtube API, so maybe that is correct.I've changed two things in your code that weren't need. I removed the extra
##
signs inxmlParse()
as it's already going to be treated as a variable there. I also moved the<cfoutput>
out of the loop, as there's a slight performance loss declaring<cfoutput>
multiple times.