Results of ColdFusion's XMLSearch functions are references

This post is more than 2 years old.

Just a quick note. I was talking with Stephen Moretti about working with the results of the XMLSearch function. He wanted to know if the results were references or copies of the XML data. I did a few tests and from what I see - they are references. Here is a quick example.

<cfxml variable="stuff"> <products> <product name="alpha"> <price>10</price> </product> <product name="beta"> <price>11</price> </product> <product name="gamma"> <price>9</price> </product> </products> </cfxml>

<cfdump var="#stuff#"> <cfset b = xmlSearch(stuff, "//product[@name='beta']")> <cfdump var="#b#"> <cfset test = b[1]> <cfset test.xmlchildren[1].xmltext =99> <cfset = "boo"> <cfdump var="#stuff#">

I begin with a simple XML packet of products. Each product has a name and a price child. I do a search to find the Beta product. Once I've got it I manipulate the name and the price. Here are the results of the dumps:

Not sure how useful this is - I almost never actually update XML - but hopefully this will help others.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by salvatore fusto posted on 1/26/2011 at 12:42 PM

in many use cases we can have an xml configuration/setup file, and often we have to change any of the items to change a behaviour, at runtime, so this post is very usefull.

Comment 2 by dmoise posted on 1/26/2011 at 5:31 PM

Good to know :)
Please note that XMLSearch can also return simple values (I <a href="!/dorin...">twitted</a> about this).

Comment 3 by Raymond Camden posted on 1/26/2011 at 5:47 PM

Ah nice - I ran into this once myself I think in an AIR application but forgot about it. :)