User Guide

Table Of Contents
856 Chapter 35: Using XML and WDDX
Creating and saving an XML document object
The following sections show the ways you can create and save an XML document object. The
specific technique that you use will depend on the application and your coding style.
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.
Creating a new XML document object using the XmlNew function
The
XmlNew function creates a new XML document object, which you must then populate. For
information on how to populate a new XML document, see Adding, deleting, and modifying
XML elements” on page 862.
Note: You cannot set the XmlDocType property for an XML document object that you create with the
XmlNew function.
The following example creates and displays the same ColdFusion document object as in
“Creating a new XML document object using the cfxml tag” on page 856.
<cfset testVar = True>
<cfscript>
MyDoc = XmlNew();
MyDoc.xmlRoot = XmlElemNew(MyDoc,"MyRoot");
if (testVar IS TRUE)
MyDoc.MyRoot.XmlText = "The value of testVar is True.";