Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with:

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




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 ?

Jon

Reply to this Comment

@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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.