问题
I have the following code.
<cfoutput>
<cfxml variable="eating">
<catalog>
<results>10</results>
<food id="bk101">
<initials>BK</initials>
<keywords>Burger King, pie, hamburgers, fries, milkshakes</keywords>
</food>
<food id="bk102">
<initials>TB</initials>
<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>CFA</initials>
<keywords>Chick-Fil-A, chicken, chicken wrap, sauce, Bananas Pudding Milkshake</keywords>
</food>
<food id="bk105">
<initials>PE</initials>
<keywords>Panda Express, rice, egg rolls, general tso</keywords>
</food>
<food id="bk106">
<initials>SJ</initials>
<keywords>Sakura Japan, rice, spring rolls, bento</keywords>
</food>
<food id="bk107">
<keywords>Five Guys, fries, burgers, hot dogs</keywords>
</food>
<food id="bk108">
<initials>TN</initials>
<keywords>Tandoori Nights, biryani, chicken, egg rolls</keywords>
</food>
<food id="bk109">
<initials>HoK</initials>
<keywords>House of Kabob, rice, bread, beef kabaob, chicken kabob</keywords>
</food>
<food id="bk110">
<initials>BF</initials>
<keywords>Baja Fresh, quesadilla, soft taco, chili con queso</keywords>
</food>
</catalog>
</cfxml>
<cfset data = queryNew("id,initials,keywords","integer,varchar,varchar")>
<cfloop index="x" from="1" to="#eating.catalog.results.xmlText#">
<cfset queryAddRow(data)>
<cfset querySetCell(data,"id",x)>
<cfset querySetCell(data,"initials","#eating.catalog.food[x].initials#")>
<cfset querySetCell(data,"keywords","#eating.catalog.food[x].keywords#")>
</cfloop>
</cfoutput>
<cfoutput query="data">
#id# - <b>#initials#</b> #keywords#<br />
</cfoutput>
You will notice the initial tags are missing for Elements 3 and 7 in the XML feed . If the initials tags are added to the XML for Elements 3 and 7, the code works beautifully. However since they are missing, this causes CFLoop to throw 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 Burger King, pie, hamburgers, fries, milkshakes
2 - TB Taco Bell, tacos, churros, burrito, gorditas
4 - CFA Chick-Fil-A, chicken, chicken wrap, sauce, Bananas Pudding Milkshake
5 - PE Panda Express, rice, egg rolls, general tso
6 - SJ Sakura Japan, rice, spring rolls, bento
8 - TN Tandoori Nights, biryani, chicken, egg rolls
9 - HoK House of Kabob, rice, bread, beef kabaob, chicken kabob
10 - BF Baja Fresh, quesadilla, soft taco, chili con queso
The above feed is a simplified example, my actual feed has hundreds of elements. With that in mind, how can I omit any Elements that give me an error, while still including the elements that do?
回答1:
Mike, I copied all your code into a local script to run a quick test. I made the one minor edit to your CFLOOP code and was about to get the output with no errors (added a conditional evaluating the struct to ensure that the key exists with structKeyExists):
<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 )>
<cfset querySetCell(data,"keywords", eating.catalog.food[x].keywords )>
</cfif>
</cfloop>
Here is the output I received in my browser:
1 - BK Burger King, pie, hamburgers, fries, milkshakes
2 - TB Taco Bell, tacos, churros, burrito, gorditas
4 - CFA Chick-Fil-A, chicken, chicken wrap, sauce, Bananas Pudding Milkshake
5 - PE Panda Express, rice, egg rolls, general tso
6 - SJ Sakura Japan, rice, spring rolls, bento
8 - TN Tandoori Nights, biryani, chicken, egg rolls
9 - HoK House of Kabob, rice, bread, beef kabaob, chicken kabob
10 - BF Baja Fresh, quesadilla, soft taco, chili con queso
来源:https://stackoverflow.com/questions/10321272/error-with-cfloop-when-entries-are-missing