Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 1078

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
Adding, deleting, and modifying XML elements
Several basic techniques exist for adding, deleting, and modifying XML elements. The example code in the technique
description uses the XML document described in
Counting and finding the position of child elements
Often, an XML element has several children with the same name. For example, in the XML document defined in the
simple XML document, the employee root element has multiple name elements.
To manipulate such an object, you often must know the number of children of the same name, and you could have to
know the position in the
XmlChildren
Counting child elements
The following user-defined function determines the number of child elements with a specific name in an element:
<cfscript>
function NodeCount (xmlElement, nodeName)
{
nodesFound = 0;
for (i = 1; i LTE ArrayLen(xmlElement.XmlChildren); i = i+1)
{
if (xmlElement.XmlChildren[i].XmlName IS nodeName)
nodesFound = nodesFound + 1;
}
return nodesFound;
}
</cfscript>
The following lines use this function to display the number of nodes named "name" in the mydoc.employee element:
<cfoutput>
Nodes Found: #NodeCount(mydoc.employee, "name")#
</cfoutput>
Determining the position of a child element with a common name
The
function determines the location in the
XmlChildPos
name. You use this index when ColdFusion must know where to insert or delete child elements. For example, if several
name elements exist in mydoc.employee, use the following code to locate name[2] in the
<cfset nameIndex = XmlChildPos(mydoc.employee, "name", 2)>
Adding an element
You can add an element by creating an element or by using an existing element.
Use the
function to create a new, empty element. This function has the following form:
XmlElemNew
XmlElemNew(docObject, elementName)
where docObject is the name of the XML document object in which you are creating the element, and elementName is
the name you are giving the new element.
Use an assignment statement with an existing element on the right side to create an element using an existing element.
See
"Copying an existing
element" on page 1075 for more information on adding elements using existing elements.
"A simple XML
document" on page 1059.
array of a specific child name that is used for multiple children.
XmlChildren
Last updated 1/20/2012
array of a specific element with a common
XmlChildren
1073
array:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents