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">
- 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. --->
- name="xmlGirls.girls.girl[ 1 ].hair"
- <!--- Param second girl node. --->
- name="xmlGirls.girls.girl[ 2 ].hair"
- <!--- Override one of the attributes. --->
- <cfset xmlGirls.girls.girl[ 2 ].hair.XmlAttributes.Style = "Short" />
- <!--- Output the updated ColdFusion XML document. --->
- 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:
| || || || || |
| || |
| || || |
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.
Looking For A New Job?
- Sr. Cold Fusion Web Developer at Vemma Nutrition
- Senior .NET Software Developer with API Experience at Marketing Holdings
- ColdFusion & AngularJS Web Developer for rapidly growing startup - at Intuvo
- Senior Coldfusion Web Developer (UK - Kent) at EPG Health Media
- Web Developer at Bridge Catalog