条目丢失时CFLoop出错

时间:2012-04-25 18:07:15

标签: coldfusion cfloop

我有以下代码。

<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 Feed中元素3和7缺少初始标记。如果将首字母标记添加到元素3和7的XML中,则代码可以很好地工作。但是,由于它们丢失,这会导致CFLoop抛出错误。

我想要做的是从结果中省略元素3(以及导致错误的所有其他条目)并防止显示任何错误,以便应用程序结果显示如此

1 - BK汉堡王,馅饼,汉堡包,薯条,奶昔

2 - TB Taco Bell,tacos,churros,burrito,gorditas

4 - CFA Chick-Fil-A,鸡肉,鸡肉裹,酱,香蕉布丁奶昔

5 - PE熊猫快递,米饭,蛋卷,一般tso

6 - SJ Sakura Japan,米饭,春卷,便当

8 - TN Tandoori Nights,biryani,鸡肉,蛋卷

9 - Hoab House of Kabob,米饭,面包,牛肉kabaob,鸡肉烤肉

10 - BF Baja Fresh,油炸玉米饼,软玉米卷,辣椒酱。

以上Feed是一个简化示例,我的实际Feed有数百个元素。考虑到这一点,我如何省略任何给我错误的元素,同时仍然包含那些元素呢?

1 个答案:

答案 0 :(得分:4)

迈克,我将所有代码复制到本地脚本中以运行快速测试。我对您的CFLOOP代码进行了一次小编辑,并且即将获得没有错误的输出(添加了条件评估结构以确保密钥与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>

以下是我在浏览器中收到的输出:

1 - BK汉堡王,馅饼,汉堡包,薯条,奶昔

2 - TB Taco Bell,tacos,churros,burrito,gorditas

4 - CFA Chick-Fil-A,鸡肉,鸡肉裹,酱,香蕉布丁奶昔

5 - PE熊猫快递,米饭,蛋卷,一般tso

6 - SJ Sakura Japan,米饭,春卷,便当

8 - TN Tandoori Nights,biryani,鸡肉,蛋卷

9 - Hoab House of Kabob,米饭,面包,牛肉kabaob,鸡肉烤肉

10 - BF Baja Fresh,油炸玉米饼,软玉米卷,辣椒酱。