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 RIA Unleashed (Nov. 2009) with:

Paraming ColdFusion XML Documents With CFParam (Part II)

By Ben Nadel on
Tags: ColdFusion

When I answering a question about my last post on using the CFParam tag to param ColdFusion XML documents, I got the idea to point something out; in my previous post, I stated that, as with any CFParam tag, there would be an overhead in creating objects that might not get used. When dealing with XML documents, you can minimize this overhead to some degree depending on how many nodes are getting paramed.

Because XML nodes in ColdFusion get inserted by copy (not by reference), we can create one stand-alone XML node for each type of node that we want to param and then use that instance within each CFParam tag:

  • <!--- Create an XML document. --->
  • <cfxml variable="xmlGirls">
  •  
  • <girls>
  • <girl>
  • <name>Libby</name>
  • <height>5'7"</height>
  • </girl>
  • <girl>
  • <name>Sarah</name>
  • <height>5'4"</height>
  • </girl>
  • </girls>
  •  
  • </cfxml>
  •  
  •  
  • <!---
  • Create a Hair node that will be used to param the
  • ColdFusion XML documents.
  • --->
  • <cfset xmlHairParam = XmlElemNew( xmlGirls, "hair" ) />
  •  
  • <!--- Set default values. --->
  • <cfset xmlHairParam.XmlAttributes.Style = "Long" />
  •  
  •  
  • <!--- Param first girl node. --->
  • <cfparam
  • name="xmlGirls.girls.girl[ 1 ].hair"
  • type="xml"
  • default="#xmlHairParam#"
  • />
  •  
  • <!--- Param second girl node. --->
  • <cfparam
  • name="xmlGirls.girls.girl[ 2 ].hair"
  • type="xml"
  • default="#xmlHairParam#"
  • />
  •  
  •  
  • <!--- Override one of the attributes. --->
  • <cfset xmlGirls.girls.girl[ 2 ].hair.XmlAttributes.Style = "Short" />
  •  
  •  
  • <!--- Output the updated ColdFusion XML document. --->
  • <cfdump
  • var="#xmlGirls#"
  • label="xmlGirls After CFParam"
  • />

NOTE: Because there are multiple Girl nodes, array notation has to be used within the CFParam tag to address each Girl node individually (otherwise only the first node in the pseudo collection would be paramed).

Notice that I am creating only one "Hair" XML node and then using it in each of the CFParam tag. While I am paraming with the aide of a single node, because ColdFusion inserts XML nodes by copy, each Girl node gets its own copy of the Hair node rather than a reference to the single node. This means we only have the overhead of creating the Hair node once even though it gets leveraged several times.

When we run the above code, we get the following CFDump output:

 
 
 
 
 
 
ColdFusion XML Document Being Paramed With CFParam Using A Single Template Node. 
 
 
 

Of course, there is still an overhead to inserting the new node in the CFParam tag; but, remember that it only gets inserted if it doesn't already exists.




Reader Comments

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.