Solved

Loop over XML in Coldfusion

Posted on 2007-03-27
2
890 Views
Last Modified: 2013-12-24
I have the following variable defined in CF:

<cfset temp="<xmlgrid><data1>aaa</data1><data2>bbb</data2></xmlgrid><xmlgrid><data1>xxx</data1><data2>yyy</data2></xmlgrid>">

How can I loop over this string and show a table containing:
aaa    bbb
xxx    yyy

Everything I have tried failed...
0
Comment
Question by:WebDvlp
2 Comments
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility

 have you tried XMLParse(temp) to transform the XML into a structure?
0
 
LVL 12

Accepted Solution

by:
mkishline earned 500 total points
Comment Utility
gdemaria is right that you'll need to use XMLParse, but first your xml needs to be formatted correctly there needs to be a top-level container around all of your data, like this:

<cfset temp="<xmlcontent><xmlgrid><data1>aaa</data1><data2>bbb</data2></xmlgrid><xmlgrid><data1>xxx</data1><data2>yyy</data2></xmlgrid></xmlcontent>" />

then you can parse your xml and access it like a struct:
<cfset xml = XMLParse(temp) />
<cfset xmlrows = XMLSearch(xml,"//xmlgrid") />

<table cellpadding="5" cellspacing="0">
<cfoutput>
<cfloop index="i" from="1" to="#ArrayLen(xmlrows)#">
      <tr><cfloop index="j" from="1" to="#ArrayLen(xmlrows[i].xmlchildren)#"><td>#xmlrows[i].xmlchildren[j].xmlText#</td></cfloop></tr>
</cfloop>
</cfoutput>
</table>
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now