Ask Ben: Changing The Root Node In A ColdFusion XML Document

<!--- Add the new XML root node around the document. --->
<cfsavecontent variable="strXmlData">
 
	<masterlist>
		#strXmlData#
	</masterlist>
 
</cfsavecontent>
 
<!---
	Now, parse the xml string into an XML document and transfer
	the child nodes to the master list root node.
--->
<cfset xmlData = XmlParse( Trim( strXmlData ) ) />
 
<!---
	Add all of the original children to the new root of the
	XML document. This creates a *copy* of the original child
	nodes, NOT a copy-by-reference!! You will lose any references
	you had to the original nodes.
 
	NOTE: This uses the undocumented AddAll() method. If you might
	want to wrap this up in a UDF, ArrayAppendAll().
--->
<cfset xmlData.XmlRoot.XmlChildren.AddAll(
	xmlData.masterlist.list.XmlChildren
	) />
 
<!--- Copy any XML attributes. --->
<cfset StructAppend(
	xmlData.XmlRoot.XmlAttributes,
	xmlData.XmlRoot.XmlChildren[ 1 ].XmlAttributes
	) />
 
<!--- Delete first child to get rid of old root node. --->
<cfset ArrayDeleteAt( xmlData.XmlRoot.XmlChildren, 1 ) />

For Cut-and-Paste