Creates a ColdFusion XML document object that contains the markup in the tag body. This tag can include XML and CFML tags. ColdFusion processes the CFML code in the tag body, and then assigns the resulting text to an XML document object variable, which is always stored in Unicode.





You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

See also

IsXmlDocIsXmlElemIsXmlRootToStringXmlChildPosXmlNewXmlParseXmlSearchXmlTransformUsing XML and WDDX in the Developing ColdFusion Applications


ColdFusion MX 7: Added support for using an XML declaration at the start of the text.

ColdFusion MX: Added this tag.









Name of the document object.




  • yes: maintains the case of document elements and attributes.
  • no


If your XML object is case sensitive, you cannot use dot notation to reference an element or attribute name. Use the name in associative array (bracket) notation, or a reference that does not use the case-sensitive name (such as xmlChildren1) instead. In the following code, the first line works with a case-sensitive XML object. The second and third lines cause errors:

MyDoc.xmlRoot.XmlAttributes.Version = "12b";
MyDoc.MyRoot.XmlAttributes["Version"] = "12b";

Use the XmlFormat function to escape special characters such as &, > and <.
To convert an XML document object back into a string, use the ToString function, at which time ColdFusion automatically prepends the <?xml version="1.0" encoding="UTF-8" ?> XML declaration.
To change the declaration to specify another encoding, use the Replace function. To specify the encoding of the text that is returned to a browser or other application, use the cfcontent tag. The following example illustrates this process:

<cfcontent type="text/xml; charset=utf-16">
<cfxml variable="xmlobject">
<name quantity="50">Belgian Waffles</name>
<description>Our famous Belgian Waffles</description>

<!--- <cfdump var="#xmlobject#">--->

<cfset myvar=toString(xmlobject)>
<cfset mynewvar=replace(myvar, "UTF-8", "utf-16")>

<!---<cfdump var="#mynewvar#">--->


The cfprocessingdirective tag prevents ColdFusion from putting white space characters in front of the XML declaration.


This following example creates a document object whose root element is MyDoc. The object includes text that displays the value of the ColdFusion variable testVar. The code creates four nested child elements, which are generated by an indexed cfloop tag. The cfdump tag displays the XML document object.

<cfxml variable="MyDoc">
<?xml version='1.0' encoding='utf-8' ?>
<cfif testVar IS True>
<cfoutput>The value of testVar is True.</cfoutput>
<cfoutput>The value of testVar is False.</cfoutput>
<cfloop index = "LoopCount" from = "1" to = "4">
This is Child node <cfoutput>#LoopCount#.</cfoutput>
<cfdump var=#MyDoc#>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy