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

Posted October 31, 2009 at 1:24 PM by Ben Nadel

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. --->
  • #girl.xmlAttributes.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:

girls.girls.girl

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:

Tricia
Joanna
Kim

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!


You Might Also Be Interested In:



Reader Comments

Oct 31, 2009 at 1:45 PM // reply »
51 Comments

That is pretty awesome, nice find!


Oct 31, 2009 at 1:47 PM // reply »
11,238 Comments

@Dan,

Yeah, CF9 is chock full of yummy goodness.


Oct 31, 2009 at 2:51 PM // reply »
26 Comments

Cool find. Thats going to help me next week!


Oct 31, 2009 at 2:54 PM // reply »
11,238 Comments

@Sam,

Good timing then.


Jon
Nov 13, 2009 at 4:46 AM // reply »
1 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 ?

Jon


Nov 15, 2009 at 7:36 PM // reply »
11,238 Comments

@Jon,

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:

array="${girls.girls.girl}"

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.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools