Modifying a ColdFusion XML object
681
{
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 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
cffile
tag
charset
attribute to specify the file’s character set. For example, if the file is encoded in UTF, specify
charset="UTF-8".
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#">
Modifying a ColdFusion XML object
As with all ColdFusion structured objects, you can often use a number of methods to change the
contents of an XML document object. For example, you often have the choice of using an
assignment statement or a function to update the contents of a structure or an array. The
following section describes the array and structure functions that you can use to modify an XML
document object. The section
“XML document object management reference” on page 683
provides a quick reference to modifying XML document object contents. Later sections describe
these methods for changing document content in detail.
Summary of Contents for ColdFusion MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......