Creating a new XML document object using the cfxml tag

The cfxml tag creates an XML document object that consists of the XML markup in the tag body. The tag body can include CFML code. ColdFusion processes the CFML code and includes the resulting output in the XML. The following example shows a simple cfxml tag:

<cfset testVar = True>
<cfxml variable="MyDoc">
   <MyDoc>
      <cfif testVar IS True>
         <cfoutput>The value of testVar is True.</cfoutput>
      <cfelse>
         <cfoutput>The value of testVar is False.</cfoutput>
      </cfif>
      <cfloop index = "LoopCount" from = "1" to = "4">
         <childNode>
            This is Child node <cfoutput>#LoopCount#.</cfoutput>
         </childNode>
      </cfloop>
   </MyDoc>
</cfxml>
<cfdump var=#MyDoc#>

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

Note: When you use the cfxml tag, do not include an <?xml ?> processing directive in the tag body. This directive is not required, and causes an error. To process XML text that includes the <?xml ?> directive, use the XmlParse function.

View comments on LiveDocs