Skip to main content
Known Participant
November 20, 2013
Question

Checking for null xml elements

  • November 20, 2013
  • 1 reply
  • 2441 views

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["SpecialId"]#

     #cfData["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["SpecialId"]#

     <cfif structKeyExists(cfData,"ImageUrl")>

          null image

     <cfelse>

          #cfData["ImageUrl"]#

     </cfif>

</cfloop>

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

    This topic has been closed for replies.

    1 reply

    p_sim
    Participating Frequently
    November 22, 2013

    I believed it should be <cfif NOT structKeyExists(cfData,"ImageUrl")>

    ggantzer2Author
    Known Participant
    November 23, 2013

    You are correct. i had it in the wrong order.