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

Posted October 31, 2009 at 1:24 PM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

That is pretty awesome, nice find!


Oct 31, 2009 at 1:47 PM // reply »
6,516 Comments

@Dan,

Yeah, CF9 is chock full of yummy goodness.


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

Cool find. Thats going to help me next week!


Oct 31, 2009 at 2:54 PM // reply »
6,516 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 »
6,516 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 Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »