ArrayAppend() Does Not Work With ColdFusion's XML Pseudo-Node-Sets

Posted December 22, 2008 at 9:27 AM by Ben Nadel

Tags: ColdFusion

I stumbled upon this the other day; at first, I was a bit confused, but the more I think about it, the more I understand that it makes perfect sense. ColdFusion's ArrayAppend() method works with XML children, but not with pseudo-XML-node-sets. Take a look at this example:

  • <!--- Define our ColdFusion XML document. --->
  • <cfxml variable="xmlData">
  •  
  • <girls>
  • <girl name="Kimmie" />
  • <girl name="Kit" />
  • <girl name="Sarah" />
  • <girl name="Jill" />
  • </girls>
  •  
  • </cfxml>
  •  
  • <!---
  • Append a duplicate girl node to the list of girl nodes.
  • By duplicating, the parent document should be handled
  • automatically.
  • --->
  • <cfset ArrayAppend(
  • xmlData.girls.girl,
  • Duplicate( xmlData.girls.girl[ 1 ] )
  • ) />
  •  
  • <!--- Dump out the new XML document. --->
  • <cfdump
  • var="#xmlData#"
  • label="xmlData After Duplicate() Append."
  • />

Here's we're just duplicating an XML node and then appending it onto the originating node list. When we run this code, we get the following ColdFusion error:

The ArrayAppend ColdFusion function is not supported on this object. The ArrayAppend ColdFusion function is not supported on an object of type coldfusion.xml.XmlNodeListWrapper. For example, XML objects can be operated on by a subset of the structure and array functions, but not all.

The problem here is that we are not using the XmlChildren collection. Rather, we are using the pseudo-node-set:

xmlData.girls.girl

What this does is collect all of the "girl" nodes into a coldfusion.xml.XmlNodeListWrapper object to allow for easier (and perhaps more meaningful) traversal. And, in an example like the one above, it might seem silly since the girl pseudo-node-set is the same as the collection of all XmlChildren nodes. However, remember that if we have mixed nodes, the pseudo-node-set will still work.

The simple fix for this is to refer to the child nodes using the XmlChildren collection:

  • <cfset ArrayAppend(
  • xmlData.girls.XmlChildren,
  • Duplicate( xmlData.girls.girl[ 1 ] )
  • ) />

This time, the code runs as expected.

So, this is a minor note; but, if you don't realize what is going on, it could really trip you up - perhaps even make you believe that you can't append XML nodes to an existing document.




Reader Comments

Apr 29, 2010 at 3:28 AM // reply »
14 Comments

Can we add a new child node that should look like the one below viz.,

<girls>
<girl name="Kimmie">
<feature>Eyes</feature>
</girl>
<girl name="Kit" />
<girl name="Sarah" />
<girl name="Jill" />
</girls>


May 3, 2010 at 9:40 AM // reply »
11,246 Comments

@Naveen,

Yeah, definitely! That's one of the beautiful things about XML - the format can be super flexible. You have have nodes that have completely different formats.


Mar 15, 2013 at 11:56 AM // reply »
3 Comments

I so appreciate your blog. You're making me look good at work. I barely knew anything about XML documents before I started my current project. But now because of you and a few others, I manipulate XML documents with the best of them! :)

Thanks!

-- Simone


Mar 15, 2013 at 11:58 AM // reply »
11,246 Comments

@Simone,

Awesome my man, glad to help! You might want to checkout my project - XDom - it makes working with XML a bit easier:

https://github.com/bennadel/XDom.cfc


Mar 19, 2013 at 4:51 PM // reply »
3 Comments

@Ben,

Will do!

-- Simone


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools