Referencing XML Attributes in ColdFusion
I was having a little problem with referencing some XML attributes in a ColdFusion XML object. Essentially, I couldn't reference them even though they were defined. My original code was:
<cfset elemAtt = xmlObej.XmlRoot.XmlChildren[r].xmlAttributes />
<cfset attName = elemAtt.name />
....
</cfloop>
When processing, ColdFusion kept returning "variable 'name' is not defined in elemAtt." So I proceeded to insert dumps, isdefineds, etc. to verify that it was there; and it was! So, after 20 or 30 minutes of working on this, it came to mind to copy the struct and then reference it. Like this:
<cfset elem = Duplicate(xmlObj.XmlRoot.XmlChildren[r].xmlAttributes) />
<cfset attName = elemAtt.name />
....
</cfloop>
This worked perfectly. Not sure why I couldn't reference it without the duplicate. It obviously had something to do with the attributes within the XML Object not really being a struct. But using the duplicate function, it copies the attribute struct as a struct (if that makes sense) and then it can be referenced normally.
I'll have to investigate the reasoning later.

There are no comments for this entry.
[Add Comment]