Modifying a ColdFusion XML object
687
If you have multiple child elements with the same name, and you want to insert a new element in
a specific position, use the
XmlChildPos
function to determine the location in the
XmlChildren
array where you want to insert the new element. For example, the following code determines the
location of mydoc.employee.name[1] and inserts a new name element as the second name
element:
<cfscript>
nameIndex = XmlChildPos(mydoc.employee, "name", 1);
ArrayInsertAt(mydoc.employee.XmlChildren, nam 1, XmlElemNew(mydoc,
"name"));
</cfscript>
Adding an element using direct assignment
You can use direct assignment to append a new element to an array of elements. You cannot use
direct assignment to insert an element into an array of elements.
When you use direct assignment, you can specify on the left side an index into the
XmlChildren
array greater than the last child in the array. For example, if there are two elements in
mydoc.employee, you can specify any number greater than two, such as
mydoc.employee.XmlChildren[6]. The element is always added as the last (in this case, third)
child.
For example, the following line appends a name element to the end of the child elements of
mydoc.employee:
<cfset mydoc.employee.XmlChildren[9] = XmlElemNew(mydoc, "name")>
If the parent element does not have any children with the same name as the new child, you can
specify the name of the new node or the left side of the assignment. For example, the following
line appends a phoneNumber element to the children of the first name element in
mydoc.employee:
<cfset mydoc.employee.name[1].phoneNumber = XmlElemNew(mydoc, "phoneNumber")>
You cannot use the node name on the left to add an element with the same name as an existing
element in the parent. For example, if mydoc.employee has two name nodes, the following line
causes an error:
<cfset mydoc.employee.name[3] = XmlElemNew(mydoc, "name")>
However, the following line does work:
<cfset mydoc.employee.XmlChilren[3] = XmlElemNew(mydoc, "name")>
Copying an existing element
You can add a copy of an existing element elsewhere in the document. For example, if there is a
mydoc.employee.name[1].phoneNumber element, but no mydoc.employee.
name[2].phoneNumber, the following line creates a new mydoc.employee. name[2].
phoneNumber element with the same value as the original element. This assignment copies the
original element. Unlike with standard ColdFusion structures, you get a true copy, not a reference
to the original structure. You can change the copy without changing the original.
<cfset mydoc.employee.name[2].phoneNumber =
mydoc.employee.name[1].phoneNumber>
Содержание ColdFusion MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...