Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Kevin Schmidt and Liz Frederick
Ben Nadel at CFUNITED 2010 (Landsdown, VA) with: Kevin Schmidt ( @schmidtkevinall ) and Liz Frederick@lizign )

Learning ColdFusion 9: Iterating Over Pseudo XML Node Wrappers With CFLoop

By Ben Nadel on
Tags: ColdFusion

For several versions of ColdFusion, we have been able to reference groups of XML nodes using pseudo node wrappers and struct and array notation. This has been a useful shorthand when extracting node data from an XML document; but, about two years ago, I found out that these pseudo node wrappers could not be used in conjunction with ColdFusion 8's (then) new CFLoop array iteration feature. In the comments to that post, however, Brad Roberts pointed out to me that this bug has been fixed in ColdFusion 9.

To test Brad's comment, I set up this quick little demo:

  • <!--- Create XML variable. --->
  • <cfxml variable="girls">
  • <girls>
  • <girl name="Tricia" />
  • <girl name="Joanna" />
  • <girl name="Kim" />
  • </girls>
  • </cfxml>
  • <!--- Loop over pseudo array xml node wrapper. --->
  • <cfloop
  • index="girl"
  • array="#girls.girls.girl#">
  • <!--- Output girl's name. --->
  •<br />
  • </cfloop>

As you can see, I am creating the XML document and then referring to the collection of "girl" nodes using the pseudo node wrapper:


I am then taking this pseudo node wrapper and iterating over it using the CFLoop tag. In ColdFusion 8, this would have thrown the following error:

Element XMLATTRIBUTES.NAME is undefined in GIRL.

However, in ColdFusion 9, this now works, and running the code above gives us the following output:


Very cool! This is just another one of the small, incremental upgrades available in ColdFusion 9 that is going to make our lives easier. The pseudo XML node wrappers already helped; but, being able to iterate over them using CFLoop's array iteration feature is really going to maximize that efficiency.

Thanks to Brad Roberts for pointing that out!

Looking For A New Job?

Ooops, there are no jobs. Post one now for only $29 and own this real estate!

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

for what stands the # in
array="#girls.girls.girl#" ?

But we determine the variables.
Is it possible to let the user input her nam, so that coldfusion gets the information like php with $_POST['inputname'] ??

Or is your example even just for the determine variables ?



The # signs are used to evaluate a ColdFusion variable. As such, the #girls.girls.girl# evaluates to the pseudo node XML wrapper and passes that to the "array" attribute.

You might see similar things in other languages like:


You could certainly use a user-defined variable to get a girl's name. You would do this in either as part of a dynamic XPath query or as part of the dot-notation turned array notation:

#girls[ FORM.inputname ].girl#

Here, the "inputname" would be a FORM variable posted by the user and used to navigate the XML document. Of course, in my example, the "inputname" would have to evaluate to "girls" in order for it to make sense.