缺少条目时,CFLoop发生错误 [英] Error with CFLoop When Entries Are Missing

查看:221
本文介绍了缺少条目时,CFLoop发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。

 < cfoutput> 
< cfxml variable =eating>
< catalog>
< results> 10< / results>
< food id =bk101>
< initials> BK< / initials>
< keywords>汉堡王,馅饼,汉堡包,炸薯条,奶昔< / keywords>
< / food>
< food id =bk102>
< initials> TB< / initials>
< keywords> Taco Bell,tacos,churros,burrito,gorditas< / keywords>
< / food>
< food id =bk103>
< keywords> Pizza Hut,披萨,奶酪,蒜味面包< / keywords>
< / food>
< food id =bk104>
< initials> CFA< / initials>
< keywords> Chick-Fil-A,鸡肉,鸡肉卷,酱汁,香蕉布丁奶昔< / keywords>
< / food>
< food id =bk105>
< initials> PE< / initials>
< keywords>熊猫快递,大米,蛋卷,一般tso< / keywords>
< / food>
< food id =bk106>
< initials> SJ< / initials>
< keywords>樱花日本,大米,春卷,bento< / keywords>
< / food>
< food id =bk107>
< keywords>五个人,炸薯条,汉堡,热狗< / 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 =xfrom =1to =#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抛出一个错误。



我想做的是从我的结果中省略Element 3(以及导致错误的所有其他条目),防止显示任何错误,从而显示应用结果像这样



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



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



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



5 - PE熊猫快递,水稻,蛋卷,一般tso



6 - SJ Sakura日本,大米,春卷, >

8 - TN Tandoori夜晚,biryani,鸡肉,蛋卷



9 - HoK House of Kabob,牛肉kabaob,鸡肉kabob



10 - BF Baja新鲜,油炸玉米饼,软炸玉米饼,辣椒粉



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

解决方案

Mike,我将所有代码复制到本地脚本中以运行快速测试。我对你的CFLOOP代码进行了一个小编辑,并且要获得没有错误的输出(添加了条件评估结构,以确保该键与structKeyExists存在):

 < cfloop index =xfrom =1to =#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 Panda Express,水稻,蛋卷,一般tso



6 - SJ Sakura日本,大米,春卷,



8 - TN Tandoori夜晚,biryani,鸡肉,蛋卷



9 - HoK House of Kabob,rice,bread,beef kabaob,chicken kabob



10 - BF Baja Fresh,quesadilla,soft taco,chili con queso


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?

解决方案

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

这篇关于缺少条目时,CFLoop发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆