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:
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:
MyDoc.employee.name[1] MyDoc.employee.XmlAttributes.Version
Instead, use names such as the following:
MyDoc.xmlRoot.XmlChildren[1] MyDoc.xmlRoot["name"][1] MyDoc.["employee"]["name"][1] MyDoc.xmlRoot.XmlAttributes["Version"] MyDoc["employee"].XmlAttributes["Version"]
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.
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].XmlAttributes.EmpType mydoc.XmlRoot.name[1].XmlAttributes["EmpType"] mydoc.XmlRoot.XmlChildren[1].XmlAttributes.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, 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.name[1].first in the following expression:
mydoc.employee.name[1].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:
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:
Assigning and retrieving CDATA values
To identify that element text is CDATA by putting it inside CDATA start and end marker information items, assign the text to the XmlCdata element, not the XmlText element. You must do this 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:
<cfscript>
myCDATA = "This is CDATA text";
MyDoc = XmlNew();
MyDoc.xmlRoot = XmlElemNew(MyDoc,"myRoot");
MyDoc.myRoot.XmlChildren[1] = XmlElemNew(MyDoc,"myChildNodeCDATA");
MyDoc.myRoot.XmlChildren[1].XmlCData = "#myCDATA#";
</cfscript>
<h3>Assigning a value to MyDoc.myRoot.XmlChildren[1].XmlCdata.</h3>
<cfoutput>
The type of element MyDoc.myRoot.XmlChildren[1] is: #MyDoc.myRoot.XmlChildren[1].XmlType#<br>
The value when output using XmlCdata is: #MyDoc.myRoot.XmlChildren[1].XmlCData#<br>
The value when output using XmlText is: #MyDoc.myRoot.XmlChildren[1].XmlText#<br>
</cfoutput>
<br>
The XML text representation of Mydoc is:
<cfoutput><XMP>#tostring(MyDoc)#</XMP></cfoutput>
<h3>Assigning a value to MyDoc.myRoot.XmlChildren[1].XmlText.</h3>
<cfset MyDoc.myRoot.XmlChildren[1].XmlText = "This is XML plain text">
<cfoutput>
The value when output using XmlCdata is: #MyDoc.myRoot.XmlChildren[1].XmlCData#<br>
The value when output using XmlText is: #MyDoc.myRoot.XmlChildren[1].XmlText#<br>
</cfoutput>
<br>
The XML text representation of Mydoc is:
<cfoutput><XMP>#tostring(MyDoc)#</XMP></cfoutput>
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/coldfusion/8/htmldocs/XML_08.html