我有以下代码。
<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>
您会注意到 XML 提要中缺少元素 3 和 7 的初始标记。如果将首字母标记添加到元素 3 和 7 的 XML 中,则代码运行良好。然而,由于它们丢失了,这会导致 CFLoop 抛出一个错误。
我想做的是从我的结果中省略元素 3(以及所有其他导致错误的条目)并防止显示任何错误,因此应用程序结果显示如下
1 - BK 汉堡王、馅饼、汉堡包、薯条、奶昔
2 - TB Taco Bell、炸玉米饼、油条、墨西哥卷饼、gorditas
4 - CFA Chick-Fil-A、鸡肉、鸡肉卷、酱汁、香蕉布丁奶昔
5-PE熊猫快递,米饭,蛋卷,左宗棠
6 - SJ Sakura Japan、米饭、春卷、便当
8 - TN Tandoori Nights、biryani、鸡肉、蛋卷
9 - HoK House of Kabob、米饭、面包、牛肉烤肉、鸡肉烤肉
10 - BF Baja Fresh、油炸玉米饼、软炸玉米饼、辣椒酱
上面的提要是一个简化的例子,我的实际提要有数百个元素。考虑到这一点,我怎样才能省略任何给我错误的元素,同时仍然包括那些给我错误的元素?