I am working with the below XML feed and am using structKeyExists and CFLoop to display the data it contains.
<cfoutput>
<cfxml variable="eating">
<catalog>
<results>10 </results>
<food id="bk101">
<initials type="thefirst" name="BK"/>
<initials type="thesecond" name="KB"/>
<keywords>Burger King, pie, hamburgers, fries, milkshakes </keywords>
</food>
<food id="bk102">
<initials type="thefirst" name="TB"/>
<initials type="thesecond" name="BT"/>
<keywords>Taco Bell, tacos, churros, burrito, gorditas </keywords>
</food>
<food id="bk103">
<keywords>Pizza Hut, pizza, cheese, garlic bread </keywords>
</food>
<food id="bk104">
<initials type="thefirst" name="CFA"/>
<initials type="thesecond" name="AFC"/>
<keywords>Chick-Fil-A, chicken, chicken wrap, sauce, Bananas Pudding Milkshake </keywords>
</food>
<food id="bk105">
<initials type="thefirst" name="PE"/>
<initials type="thesecond" name="EP"/>
<keywords>Panda Express, rice, egg rolls, general tso </keywords>
</food>
<food id="bk106">
<initials type="thefirst" name="SJ"/>
<initials type="thesecond" name="JS"/>
<keywords>Sakura Japan, rice, spring rolls, bento </keywords>
</food>
<food id="bk107">
<initials type="thefirst" name="FG"/>
<keywords>Five Guys, fries, burgers, hot dogs </keywords>
</food>
<food id="bk108">
<initials type="thefirst" name="TN"/>
<initials type="thesecond" name="NT"/>
<keywords>Tandoori Nights, biryani, chicken, egg rolls </keywords>
</food>
<food id="bk109">
<initials type="thefirst" name="HoK"/>
<keywords>House of Kabob, rice, bread, beef kabaob, chicken kabob </keywords>
</food>
<food id="bk110">
<initials type="thefirst" name="BF"/>
<initials type="thesecond" name="FB"/>
<keywords>Baja Fresh, quesadilla, soft taco, chili con queso </keywords>
</food>
</catalog>
</cfxml>
</cfoutput>
<cfset data = queryNew("id,initials,initials2,keywords","integer,varchar,varchar,varchar")>
<cfloop index="x" from="1" to="#eating.catalog.results.xmlText#">
<cfif structKeyExists(eating.catalog.food[x],"initials")>
<cfset queryAddRow(data)>
<cfset querySetCell(data,"id",x)>
<cfset querySetCell(data,"initials", eating.catalog.food[x].initials[1].xmlattributes.name )>
<cfset querySetCell(data,"initials2", eating.catalog.food[x].initials[2].xmlattributes.name )>
<cfset querySetCell(data,"keywords", eating.catalog.food[x].keywords )>
</cfif>
</cfloop>
<cfoutput query="data">
#id# - #initials# :: #initials2# :: #keywords# <br /><br />
</cfoutput>
You will notice there is one initial tag missing for Element 3 and two that are missing for Elements 7 and 9 in the XML feed . If the initials tags are added to the XML for Elements 3,7,9, the code works beautifully. However since they are missing, this causes an error to be thrown out an error.
What I would like to do is omit Element 3 (and all other entries which cause an error) from my results and prevent any errors from showing so the application result shows up like so:
1 – BK :: KB :: Burger King, pie, hamburgers, fries, milkshakes
2 – TB :: BT :: Taco Bell, tacos, churros, burrito, gorditas
4 – CFA :: AFC :: Chick-Fil-A, chicken, chicken wrap, sauce, Bananas Pudding Milkshake
5 – PE :: EP :: Panda Express, rice, egg rolls, general tso
6 – SJ :: JS :: Sakura Japan, rice, spring rolls, bento
8 – TN :: NT :: Tandoori Nights, biryani, chicken, egg rolls
10 – BF :: FB :: Baja Fresh, quesadilla, soft taco, chili con queso
Note my example is simplified, in actuality I am working with an XML feed with hundreds of elements. With that in mind, what am I doing wrong and how can I get the above to display correctly?
StructKeyExists() works just fine, but you need to check to ensure the second initials exists too. Adding ArrayLen(eating.catalog.food[x].initials) GT 1 (or EQ 2, if you know it’ll always be 2) will solve it.
With this fix, the example you gave outputs 1, 2, 4, 5, 6, 8, and 10. If you want to print 7 and 9, just move the check to here: