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:
- Using the element names, such as mydoc.employee.name1
- Using the corresponding structure entry names (that is, XmlChildren array entries), such as mydoc.employee.XmlChildren1
Similarly, you can use either, or a combination of both, of the following notation methods:
- Structure (dot) notation, such as mydoc.employee
- Associative array (bracket) notation, such as mydoc"employee"
Referencing the contents of an XML object
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:
- By default, ColdFusion ignores element name case. As a result, it considers the element name MyElement and the element name myELement to be equivalent. To make element name matching case-sensitive, specify CaseSensitive="True" in the cfxml tag, or specify True as a second argument in the XmlParse or XmlNew function that creates the document object.
If your XML object is case sensitive, do not use dot notation to reference an element or attribute name. Use the name in associative array (bracket) notation, or a reference that does not use the case-sensitive name. For example, do not use names such as the following:
Instead, use names such as the following:
Note: Because ColdFusion_ always treats variable names as case-insensitive, using dot notation for element and attribute names in a case-sensitive XML document can generate unexpected results (such as all-uppercase variable names), exceptions, or both._
- If your XML object is case sensitive, you cannot use dot notation to reference an element or attribute name. Use the name in associative array (bracket) notation, or a reference that does not use the case-sensitive name (such as XmlChildren1) instead.
- Use an array index to specify one of multiple elements with the same name; for example, #mydoc.employee.name1 and #mydoc.employee.name2.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.
- Use an array index into the XmlChildren array to specify an element without using its name; for example, mydoc.XmlRoot.XmlChildren1.
- Use associative array (bracket) notation to specify an element name that contains a period or colon; for example, myotherdoc.XmlRoot["Type1.Case1"].
- You can use DOM methods in place of structure entry names.
For example, the following variables all reference the XmlText value "Almanzo" in the XML document created in A simple XML document:
The following variables all reference the EmpType attribute of the first name element in the XML document created in A simple XML document:
Neither of these lists contains a complete set of the possible combinations that can make up a reference to the value or attribute.
Assigning data to an XML object
When you use an XML object reference on the left side of an expression, most of 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.name1.first in the following expression:
mydoc.employee.name.first.MyNewElement = XmlElemNew(mydoc, NewElement);
The rule for naming in case correct document objects, however, applies to the full reference string, as indicated by the following caution:
Note: Because ColdFusion_ always treats variable names as case-insensitive, using dot notation for element and attribute names in a case-sensitive XML document can generate unexpected results (such as all-uppercase variable names), exceptions, or both. In case-sensitive XML documents, use associative array notation or DOM notation names (such as XmlRoot or XmlChldren2)._
Referencing the last element on the left side of an expression
The following rules apply to the meaning of the last component on the left side of an expression:
The component name is an element structure key name (XML property name), such as 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.name1.first element to "This is a comment":
mydoc.employee.name.first.XmlComment = "This is a comment";
If the component name specifies an element name and does not end with a numeric index, for example mydoc.employee.name, ColdFusion assigns the value on the right of the expression to the first matching element. For example, if both mydoc.employee.name1 and mydoc.employee.name2 exist, the following expression replaces mydoc.employee.name1with 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.name1 and mydoc.employee.name2, now only one mydoc.employee.name element exists with the contents of the original mydoc.employee.name2.
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 an 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 a, mydoc.employee.name.phoneNumber element does not exist, the following expression creates an mydoc.employee.name.phoneNumberelement:
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, no mydoc.employee.phoneNumberelement exists, 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 placing it inside CDATA start and end marker information items, assign the text to the XmlCdata element, not the XmlText element. Specify CDATA 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: