User Guide

Table Of Contents
Creating and saving an XML document object 857
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#>
Creating an XML document object from existing XML
The
XmlParse function converts an XML document or document fragment represented as text
into a ColdFusion document object. You can use a string variable containing the XML or the
name or URL of a file that contains the text. 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)>
The following example converts an XML text document is in a file to an XML document object:
C:\temp\myxmldoc.xml" variable="XMLFileText">
<cfset myXMLDocument=XmlParse(“C:\temp\myxmldoc.xml" variable=")>
The XmlParse function takes a second, optional, attribute that specifies whether to maintain the
case of the elements and attributes in the document object. The default is to have the document
object be case-insensitive. For more information on case-sensitivity, see “Referencing the contents
of an XML object” on page 852.
The
XmlParse function also lets you specify a DTD or Schema to validate the XML text; if the
XML is not valid, ColdFusion MX generates an error. You can specify the filename or URL of the
validator, or the DTD or Schema can be in a CFML variable. You can also tell ColdFusion MX to
use a DTD or Schema that is identified in the XML text. If you specify validation, you must also
specify whether the document is be case-sensitive. The following example validates an XML
document on file using a DTD that it specifies using a URL:
myDoc=XMLParse("C:\CFusionMX7\wwwroot\examples\custorder.xml", false,
"http://localhost:8500/examples/custorder.dtd")>
Saving and exporting an XML document object
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#">