I came up with a little short cut earlier today when I was commenting on an article Ben Nadel wrote and he thought it was pretty clever, so I thought other people might find it useful as well.

Before ColdFusion 8, if I was reading an XML document, I would use XMLSearch to create an array, then use cfloop to process each node that matched my xmlSearch, like this:

<cfxml variable="MyXMLDoc">
<?xml version='1.0' encoding='utf-8' ?>
<books>
   <book>
      <isbn>0321330110</isbn>
      <title>Macromedia ColdFusion MX 7 Certified Developer Study Guide</title>
      <author>Ben Forta</author>
   </book>
   <book>
      <isbn>0596004206</isbn>
      <title>Learning XML, Second Edition</title>
      <author>Erik Ray</author>
   </book>
<book>
      <isbn>0782140297</isbn>
      <title>Coldfusion MX Developer's Handbook</title>
      <author>Raymond Camden</author>
   </book>
</books>
</cfxml>

<!--- get an array of book nodes using xmlSearch --->
<cfset BookNodes = xmlSearch(MyXMLDoc,'/books/book')>

<!--- loop through the array and output the data --->
<cfoutput>
<cfloop from="1" to="#arraylen(BookNodes)#" index="i">

<cfset BookXML = xmlparse(BookNodes[i])>
<b>ISBN:</b> #BookXML.book.isbn.xmltext#<br>
<b>Title:</b> #BookXML.book.title.xmlText#<br>
<b>Author:</b> #BookXML.book.author.xmlText#<br><br>
</cfloop>
</cfoutput>

With the new "Array" attribute in ColdFusion 8's CFLoop tag, you can combine that two step process in to one CFLoop tag by placing the xmlSearch() function within the array attribute like this:

<cfxml variable="MyXMLDoc">
<?xml version='1.0' encoding='utf-8' ?>
<books>
   <book>
      <isbn>0321330110</isbn>
      <title>Macromedia ColdFusion MX 7 Certified Developer Study Guide</title>
      <author>Ben Forta</author>
   </book>
   <book>
      <isbn>0596004206</isbn>
      <title>Learning XML, Second Edition</title>
      <author>Erik Ray</author>
   </book>
<book>
      <isbn>0782140297</isbn>
      <title>Coldfusion MX Developer's Handbook</title>
      <author>Raymond Camden</author>
   </book>
</books>
</cfxml>

<cfoutput>
<!---
   Loop over the array using a ColdFusion's new Array
   iteration loop, using XMLSearch to create the array on the fly.
--->

<cfloop
   index="BookXML"
   array="#xmlsearch(MyXMLDoc,'/books/book')#">

   
   <cfset BookXML = xmlparse(BookXML)>
   
   <!--- Output the data. --->
   <b>ISBN:</b> #BookXML.book.isbn.xmltext#<br>
   <b>Title:</b> #BookXML.book.title.xmlText#<br>
   <b>Author:</b> #BookXML.book.author.xmlText#<br><br>
</cfloop>
</cfoutput>

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Ben Nadel's Gravatar Oh snap, you beat me to it :)

http://www.bennadel.com/index.cfm?dax=blog:1104.vi...

Awesome tip Scott.
# Posted By Ben Nadel | 12/27/07 9:54 AM
Chad's Gravatar This post helped me traverse through xml in CF MX 7. Thanks!
# Posted By Chad | 10/10/08 4:06 PM
Tomas Sancio's Gravatar Thank you very much for this post. It saved me a considerable amount of time. Thanks to Ben, too for all his helpful information!
# Posted By Tomas Sancio | 7/3/11 7:59 PM
Will B.'s Gravatar Just FYI, I was taught this years ago, and it might be language dependent, but was told it's a bad practice to put a function call into the loop condition. Each time the loop iterates, in theory, it has to re-run the function to get the data. I make it a "best practice" to place the function result into a local variable, *then* use that variable in my loop condition. In this case:

<cfset local.aBooks = xmlsearch(MyXMLDoc,'/books/book') />
<cfloop
index="BookXML"
array="#local.aBooks#">
[....]
</cfloop>
# Posted By Will B. | 9/17/15 12:35 PM