
1073
DEVELOPING
COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
Las
t
upda
te
d 8/5/2010
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 “
A simple XML document
” on page 1059.
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
array of a specific child name that is used for multiple children.
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 = node 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
XmlChildPos
function determines the location in the
XmlChildren
array of a specific element with a common
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
XmlChildren
array:
<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
XmlElemNew
function to create a new, empty element. This function has the following form:
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.
Содержание COLDFUSION 9
Страница 1: ...Developing Applications ADOBE COLDFUSION 9...