0

我正在使用以下 XML 提要并使用 structKeyExists 和 CFLoop 来显示它包含的数据。

<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>

您会注意到 XML 提要中元素 3 缺少一个初始标记,元素 7 和 9 缺少两个初始标记。如果将缩写标签添加到 Elements 3、7、9 的 XML 中,则代码运行良好。但是,由于它们丢失,这会导致错误被抛出一个错误。

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

1 - BK :: KB :: 汉堡王、馅饼、汉堡、薯条、奶昔

2 - TB :: BT :: 塔可钟、玉米饼、油条、墨西哥卷饼、gorditas

4 - CFA :: AFC :: Chick-Fil-A、鸡肉、鸡肉卷、酱汁、香蕉布丁奶昔

5 - PE :: EP :: 熊猫快递,米饭,蛋卷,左宗棠

6 - SJ :: JS :: 日本樱花,米饭,春卷,便当

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

10 - BF :: FB :: Baja Fresh、油炸玉米饼、软炸玉米饼、辣椒酱

请注意,我的示例已简化,实际上我正在使用包含数百个元素的 XML 提要。考虑到这一点,我做错了什么,我怎样才能让上面的内容正确显示?

4

1 回答 1

5

StructKeyExists() 工作得很好,但您需要检查以确保第二个首字母也存在。添加 ArrayLen(eating.catalog.food[x].initials) GT 1 (或 EQ 2,如果你知道它总是 2)将解决它。

<cfif structKeyExists(eating.catalog.food[x],"initials") AND ArrayLen(eating.catalog.food[x].initials) GT 1>

通过此修复,您给出的示例输出 1、2、4、5、6、8 和 10。如果要打印 7 和 9,只需将检查移至此处:

<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 )>
    <cfif ArrayLen(eating.catalog.food[x].initials) GT 1>
        <cfset querySetCell(data,"initials2", eating.catalog.food[x].initials[2].xmlattributes.name )>
    </cfif>
    <cfset querySetCell(data,"keywords", eating.catalog.food[x].keywords )>
 </cfif>
于 2012-04-27T21:34:41.240 回答