Copy link to clipboard
Copied
Hi everyone,
I’m working on a small food-related project in ColdFusion where I need to pull menu data from an XML feed. The feed has both restaurant names and menu items mixed into one "keywords" field.
Here’s a simplified version of the XML:
<cfxml variable="menu">
<catalog>
<food id="cs101">
<author>Chicken</author>
<keywords>Chicken Salad Chick, Classic Carol, Lauryn’s Lemon Basil, Cranberry Kelli</keywords>
</food>
<food id="cs102">
<author>Deli</author>
<keywords>Subway, Italian BMT, Turkey Breast, Tuna, Veggie Delight</keywords>
</food>
</catalog>
</cfxml>
What I’d like to do is just display the restaurant name (always the first value in the keyword string) and ignore the rest.
Desired Output:
Chicken Salad Chick
Subway
For context, the real data comes from a food blog project I’m building: https://chickensaladchicksmenu.com
Any help or code snippet would be awesome 🙏
You can solve this by just taking the first item from the keywords list. ColdFusion has a built-in function for that:
<cfxml variable="menu">
<catalog>
<food id="cs101">
<author>Chicken</author>
<keywords>Chicken Salad Chick, Classic Carol, Lauryn’s Lemon Basil, Cranberry Kelli</keywords>
</food>
<food id="cs102">
<author>Deli</author>
<keywords>Subway, Italian BMT, Turkey Breast, Tuna, Veggie Delight</keywords>
</food>
</catalog>
</cfxml>
<cfset xmlDoc = menu>
<cfset foods = xmlSearch(xmlDoc, "//food")>
<cfou
...Copy link to clipboard
Copied
You can solve this by just taking the first item from the keywords list. ColdFusion has a built-in function for that:
<cfxml variable="menu">
<catalog>
<food id="cs101">
<author>Chicken</author>
<keywords>Chicken Salad Chick, Classic Carol, Lauryn’s Lemon Basil, Cranberry Kelli</keywords>
</food>
<food id="cs102">
<author>Deli</author>
<keywords>Subway, Italian BMT, Turkey Breast, Tuna, Veggie Delight</keywords>
</food>
</catalog>
</cfxml>
<cfset xmlDoc = menu>
<cfset foods = xmlSearch(xmlDoc, "//food")>
<cfoutput>
<cfloop array="#foods#" index="f">
#trim(listFirst(f.keywords.xmlText, ","))#<br>
</cfloop>
</cfoutput>
Output:
Chicken Salad Chick
Subway
That should work fine even if your XML has hundreds of <food> nodes.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now