Here is an interesting little issue I ran into. Given the following simple XML (and I'm typing this on the fly so pardon any typos), imagine you want to loop over the people nodes:
<root>
<people />
<people />
<people />
</root>
ColdFusion allows you to treat the people nodes as an array. You can access the second people node by using xmlVar.people[2]. Most folks though will typically want to iterate over each person. Using CFLOOP and it's new array syntax, you might do it like so:
<cfloop index="person" array="#people#">
do stuff
</cfloop>
While this works perfectly well in ColdFusion 9, in ColdFusion 8 it fails. The person object is a Java object of the class org.apache.xerces.dom.DeferredElementNSImpl. Now I won't pretend to know how that differs exactly from ColdFusion 9, but the point is, you can't use it in the same way you can with ColdFusion 9.
Of course, the fix for ColdFusion 8 is trivial - change your cfloop to:
<cfloop index="x" from="1" to="#arrayLen(people)#">
<cfset person = people[x]>
Archived Comments
Does isArray(people) return true?
To be honest, I did not test, but in both cases, the server treated it as an array - the difference was in how it got the element. If that makes sense.
I've done some cf xml work recently, so while I'm in that mood I did some related tests.
<cfxml variable="xml">
<root>
<people>Ray</people>
<people>Bay</people>
<people>Lay</people>
</root>
</cfxml>
<cfoutput>
#isArray(xml.xmlRoot.people)#<br>
#HTMLEditFormat(xml.xmlRoot.people[1])#<br>
#isArray(xml.xmlRoot.xmlChildren)#<br>
#HTMLEditFormat(xml.xmlRoot.xmlChildren[1])#<br>
<cfloop collection="#xml.xmlRoot#" item="person">
- #HTMLEditFormat(xml.xmlRoot[person].xmlText)#<br>
</cfloop>
</cfoutput>
OUTPUT:
NO
<?xml version="1.0" encoding="UTF-8"?> <people>Ray</people>
YES
<?xml version="1.0" encoding="UTF-8"?> <people>Ray</people>
- Ray
- Ray
- Ray
Most interesting is that isArray(xml.xmlRoot.people) is False, while isArray(xml.xmlRoot.xmlChildren) is True.
Anyway I avoid accessing nodes by their names.
I don't have CF8 installed but cosmic forces telling me the result would be the same :)
Another way to get around this limitation in CF8 is to pass an XmlSearch() result to the array attribute in CFLoop.
Saved my day, not first, not last. Thanks again !