Solved

Loop over XML in Coldfusion

Posted on 2007-03-27
2
892 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
ID: 18800046

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

Accepted Solution

by:
mkishline earned 500 total points
ID: 18800356
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

13 Experts available now in Live!

Get 1:1 Help Now