Because an XML document object is represented as a structure, you can access XML document contents using either, or a combination of both, of the following ways:
XmlChildren
array entries), such as mydoc.employee.XmlChildren[1]Similarly, you can use either, or a combination of both, of the following notation methods:
Use the following rules when you reference the contents of an XML document object on the right side of an assignment or as a function argument:
CaseSensitive="True"
in the cfxml
tag, or specify True
as a second argument in the XMLNew
or XMLParse
function that creates the document object.
If you omit the array index on the last component of an element identifier, ColdFusion treats the reference as the array of all elements with the specified name. For example, mydoc.employee.name refers to an array of two name elements.
XmlChildren
array to specify an element without using its name; for example, mydoc.XmlRoot.XmlChildren[1].myotherdoc.XmlRoot["Type1.Case1"]
.For example, the following variables all refer to the XmlText value "Almanzo" in the XML document created in "A simple XML document":
mydoc.XmlRoot.XmlChildren[1].XmlChildren[1].XmlText
mydoc.employee.name[1].first.XmlText mydoc.employee.name[1]["first"].XmlText mydoc["employee"].name[1]["first"].XmlText mydoc.XmlRoot.name[1].XmlChildren[1]["XmlText"]
The following variables all refer to the EmpType attribute of the first name element in the XML document created in "A simple XML document" :
mydoc.employee.name[1].XmlAttributes.EmpType
mydoc.employee.name[1].XmlAttributes["EmpType"] mydoc.employee.XmlChildren[1].EmpType mydoc.XmlRoot.name[1].XmlAttributes["EmpType"] mydoc.XmlRoot.XmlChildren[1].EmpType
Neither of these lists contains a complete set of the possible combinations that can make up a reference to the value or attribute.
When you use an XML object reference on the left side of an expression, the preceding rules apply to the reference up to the last element in the reference string.
For example, the rules in "Referencing the contents of an XML object" apply to mydoc.employee.name[1].first in the following expression:
mydoc.employee.name[1].first.MyNewElement = XmlElemNew(mydoc, NewElement);
The following rules apply to the meaning of the last component on the left side of an expression:
XmlComment
, ColdFusion sets the value of the specified element structure entry to the value of the right side of the expression. For example, the following line sets the XML comment in the mydoc.employee.name[1].first element to "This is a comment":
mydoc.employee.name[1].first.XmlComment = "This is a comment";
mydoc.employee.name
, ColdFusion assigns the value on the right of the expression to the first matching element. For example, if both mydoc.employee.name[1]
and mydoc.employee.name[2]
exist, the following expression replaces mydoc.employee.name[1]
with a new element named address, not an element named name:
mydoc.employee.name = XmlElemNew(mydoc, "address");
After executing this line, if there had been both mydoc.employee.name[1]
and mydoc.employee.name[2]
, there is now only one mydoc.employee.name
element with the contents of the original mydoc.employee.name[2]
.
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");
mydoc.employee.phoneNumber
element, the following expression creates a phoneNumber element containing an AreaCode element:mydoc.employee.name.phoneNumber.AreaCode = XmlElemNew(mydoc, "AreaCode");