
Using an XML object
679
•
If the component name does not match an existing element, the element names on the left and
right sides of the expression must match. ColdFusion creates a new element with the name of
the element on the left of the expression. If the element names do not match, it generates an
error.
For example if there is no
mydoc.employee.name.phoneNumber
element, the following
expression creates a new
mydoc.employee.name.phoneNumber
element:
mydoc.employee.name.phoneNumber = XmlElemNew(mydoc, "phoneNumber");
The following expression causes an error:
mydoc.employee.name.phoneNumber = XmlElemNew(mydoc, "address");
•
If the component name does not match an existing element and the component’s parent or
parents also do not exist, ColdFusion creates any parent nodes as specified on the left side and
use the previous rule for the last element. For example, if there is no
mydoc.employee.phoneNumber
element, the following expression creates a phoneNumber
element containing an AreaCode element:
mydoc.employee.name.phoneNumber.AreaCode = XmlElemNew(mydoc, "AreaCode");
Assigning and retrieving CDATA values
To identify that element text is CDATA by putting it inside CDATA start and end marker
information items, assign the text to the XmlCdata element, not the XmlText element. You must
do this because ColdFusion escapes the < and > symbols in the element text when you assign it to
an XmlText entry. You can assign a value to an element’s XmlText entry
or
its XmlCdata entry,
but not to both, as each assignment overwrites the other.
When you retrieve data from the document object, references to XmlCdata and XmlText return
the same string.
The following example shows how ColdFusion handles CDATA text:
<cfscript>
myCDATA = "This is CDATA text";
MyDoc = XmlNew();
MyDoc.xmlRoot = XmlElemNew(MyDoc,"myRoot");
MyDoc.myRoot.XmlChildren[1] = XmlElemNew(MyDoc,"myChildNodeCDATA");
MyDoc.myRoot.XmlChildren[1].XmlCData = "#myCDATA#";
</cfscript>
<h3>Assigning a value to MyDoc.myRoot.XmlChildren[1].XmlCdata.</h3>
<cfoutput>
The type of element MyDoc.myRoot.XmlChildren[1] is:
#MyDoc.myRoot.XmlChildren[1].XmlType#<br>
The value when output using XmlCdata is:
#MyDoc.myRoot.XmlChildren[1].XmlCData#<br>
The value when output using XmlText is:
#MyDoc.myRoot.XmlChildren[1].XmlText#<br>
</cfoutput>
<br>
The XML text representation of Mydoc is:
<cfoutput><XMP>#tostring(MyDoc)#</XMP></cfoutput>
<h3>Assigning a value to MyDoc.myRoot.XmlChildren[1].XmlText.</h3>
<cfset MyDoc.myRoot.XmlChildren[1].XmlText = "This is XML plain text">
<cfoutput>
The value when output using XmlCdata is:
#MyDoc.myRoot.XmlChildren[1].XmlCData#<br>
Summary of Contents for COLDFUSION MX 61-DEVELOPING 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: ......