XML数据的structKeyExists不起作用

时间:2012-04-27 21:19:29

标签: coldfusion

我正在使用下面的XML feed并使用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>

您会注意到元素3中缺少一个初始标记,而XML Feed中的元素7和9缺少两个初始标记。如果将首字母标记添加到XML for Elements 3,7,9中,则代码可以很好地工作。但是,由于它们丢失了,这会导致错误抛出错误。

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

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

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

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

5 - PE :: EP :: Panda Express,米饭,蛋卷,一般tso

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

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

10 - BF :: FB :: Baja Fresh,油炸玉米饼,软玉米卷,辣椒调味品

请注意,我的示例已经过简化,实际上我正在使用包含数百个元素的XML Feed。考虑到这一点,我做错了什么,如何才能正确显示上述内容?

1 个答案:

答案 0 :(得分: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>