Cold Fusion: Way to set default values for XML elements?
I know that XML should all be standardized so all elements always exist. However, this client is using an old Oracle system which doesn't always include all the XML elements. In fact it leaves them out when they're null.
So, I wrote the following function to work like a cfparam for XML elements:
<cfargument name="scope" type="xml">
<cfargument name="name" type="string">
<cfargument name="default" type="string">
<cfset var result = "">
<cftry>
<cfset result = Trim(Evaluate("scope.#Arguments.name#"))>
<cfcatch type="Expression"><cfset result = default></cfcatch>
</cftry>
<cfreturn result>
</cffunction>
While, I REALLY hate using a cftry to do this, I can't see any other way. Perhaps you know one??

<cfif IsDefined("scope.name")>
It's defined
<cfelse>
It is not defined
</cfif>
If you're drilling down into a complicated xml document, with structures embedded in arrays embedded in arrays that include structures and so on then IsDefined won't work (but I suspect that the xmlParam function will run into the same problems with complicated xml documents).
I recall, however, a suggestion you made to me at the last Hartford CFUG meeting, about this topic. You suggested that I loop over the XML, perhaps comparing it with the elements I was requiring and/or putting it into a manual strucutre and then running regular structure tags off it. Being on a limited budget with the client, I didn't try either, but I think they're great idea and probably would be a more solid way of doing this.
I really wonder if Adobe will be adding some sort of a CFPARAM or IsDefined() equivalent for XML documents.
i'm not sure if your specific situation, but in my tests, IsDefined and StructKeyExists appear to be working. Here is some code:
<cfhttp url="http://www.jeffryhouser.com/rss.cfm?mode=full"... timeout="30"></cfhttp>
<cfset xml = xmlparse(cfhttp.filecontent)>
<cfif IsDefined("xml.arg.channel")>
Element is Defined<Br>
<cfelse>
Not defined<bR>
</cfif>
<cfif IsDefined("xml.arg")>
Element is Defined<Br>
<cfelse>
Not defined<bR>
</cfif>
<cfif IsDefined("xml.rss")>
Element is Defined<Br>
<cfelse>
Not defined<bR>
</cfif>
<cfif StructKeyExists(xml,'rss')>
Element is Defined<Br>
<cfelse>
Not defined<bR>
</cfif>
<cfif StructKeyExists(xml,'arg')>
Element is Defined<Br>
<cfelse>
Not defined<bR>
</cfif>
<cfif StructKeyExists(xml.rss,'channel')>
Element is Defined<Br>
<cfelse>
Not defined<bR>
</cfif>
Is this different than your situation?
<cfif StructKeyExists(myXml.RootNode.ChildNode.SomeNodeArray[1]["ChildNodeInArray"], "NameOfNodeIAmLookingFor")>
Hurray!
<cfelse>
Booo
</cfif>