The following sections show the ways you can create and save an XML document object. The specific technique you use will depend on the application and your coding style.
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.
The XmlNew
function creates a new XML document object, which you must then populate. The following example creates and displays the same ColdFusion document object as in "Creating a new XML document object using the cfxml tag":
<cfset testVar = True>
<cfscript> MyDoc = XmlNew(); MyDoc.xmlRoot = XmlElemNew(MyDoc,"MyRoot"); if (testVar IS TRUE) MyDoc.MyRoot.XmlText = "The value of testVar is True."; else MyDoc.MyRoot.XmlText = "The value of testVar is False."; for (i = 1; i LTE 4; i = i + 1) { MyDoc.MyRoot.XmlChildren[i] = XmlElemNew(MyDoc,"childNode"); MyDoc.MyRoot.XmlChildren[i].XmlText = "This is Child node " & i &"."; } </cfscript> <cfdump var=#MyDoc#>
The XmlParse
function converts an XML document or document fragment represented as a text string into a ColdFusion document object.
If the XML document is already represented by a string variable, use the XmlParse
tag directly on the variable. For example, if your application uses cfhttp action="get"
to get the XML document, use the following line to create the XML document object:
<cfset myXMLDocument = XmlParse(cfhttp.fileContent)>
If the XML document is in a file, use cffile
convert the file to a CFML variable, then use the XmlParse
tag on the resulting variable. For example, if the XML document is in the file C:\temp\myxmldoc.xml, use the following code to convert the file to an XML document object:
<cffile action="read" file="C:\temp\myxmldoc.xml" variable="XMLFileText">
<cfset myXMLDocument=XmlParse(XMLFileText)>
Note: If the file is not encoded with the ASCII or Latin-1 character set, use the cffil
tag charset
attribute to specify the file's character set. For example, if the file is encoded in UTF, specify charset="UTF-8".
The ToString
function converts an XML document object to a text string. You can then use the string variable in any ColdFusion tag or function.
To save the XML document in a file, use the ToString
function to convert the document object to a string variable, then use the cffile
tag to save the string as a file. For example, use the following code to save the XML document myXMLDocument in the file C:\temp\myxmldoc.xml:
<cfset XMLText=ToString(myXMLDocument)>
<cffile action="write" file="C:\temp\myxmldoc.xml" output="#XMLText#">