Paraming ColdFusion XML Documents With CFParam (Part II)

Posted May 4, 2009 at 10:21 AM

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:

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

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

Download Code Snippet ZIP File

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





Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
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 »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »