2 Replies Latest reply on Nov 23, 2013 6:52 AM by ggantzer2

    Checking for null xml elements

    ggantzer2

      Hello,

       

      i am parsing an xml file by using a CFLoop. Some rows in the file do not contain every element. What is the best way to check if an element exists?

       

      My current code looks something like the following:

       

      <cfloop from="1" to="#arrayLen(cfData)#" index="i">

           #cfData[i]["SpecialId"]#

           #cfData[i]["ImageUrl"]#

      </cfloop>

       

      For some rows, ImageUrl exists, but it doesnt in others. When running the code, i received the following error:

      Element ImageUrl is undefined in a CFML structure referenced as part of an expression.

      I tried the following:

       

      <cfloop from="1" to="#arrayLen(cfData)#" index="i">

           #cfData[i]["SpecialId"]#

           <cfif structKeyExists(cfData[i],"ImageUrl")>

                null image

           <cfelse>

                #cfData[i]["ImageUrl"]#

           </cfif>

      </cfloop>

       

      However, this returns the same error. Is there an issue with the way i am using StructKeyExists?