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には1つの初期タグがあり、要素7と9には2つの初期タグがありません。要素3、7、9のXMLにイニシャルタグを追加すると、コードは美しく機能します。ただし、これらが欠落しているため、エラーがスローされます。

私がやりたいのは、結果から要素3(およびエラーを引き起こす他のすべてのエントリ)を省略し、エラーが表示されないようにして、アプリケーションの結果が次のように表示されるようにすることです。

1-BK :: KB ::バーガーキング、パイ、ハンバーガー、フライドポテト、ミルクセーキ

2-TB :: BT ::タコベル、タコス、チュロス、ブリトー、ゴルディータ

4-CFA :: AFC ::チックフィレイ、チキン、チキンラップ、ソース、バナナプディングミルクセーキ

5-PE :: EP ::パンダエクスプレス、ライス、エッグロール、左宗棠

6 --SJ :: JS ::さくらジャパン、ご飯、春巻き、お弁当

8-TN :: NT ::タンドリーナイト、ビリヤニ、チキン、エッグロール

10-BF :: FB ::バハフレッシュ、ケサディーヤ、ソフトタコス、チリコンケソ

私の例は単純化されていることに注意してください。実際には、数百の要素を含むXMLフィードを使用しています。それを念頭に置いて、私は何を間違っているのですか、そしてどうすれば上記を正しく表示させることができますか?

4

1 に答える 1

5

StructKeyExists()は問題なく機能しますが、2番目のイニシャルも存在することを確認する必要があります。ArrayLen(eating.catalog.food [x] .initials)GT 1(または、常に2になることがわかっている場合はEQ 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 に答える