Below is a small piece of code that I thought might be useful to any of you
working with cf_soxml.  You may have run into the frustration that sometimes
cf_soxml creates an array if it sees multiple nodes of the same name, but if
there's only one node, it creates a structure.  Therefore, you're code has
to determine if it's an array or not and process accordingly.  This function
is used to ensure that a node is always an array.

Hope this is helpful to someone,

-Nelson



Example 1
***********************
<book>
    <author>Jane Smith</author>
    <author>John Doe</author>
</book>

Example 2
***********************
<book>
    <author>Jane Smith</author>
</book>

In Example 1, "xmlStruct.book.author" is an array whereas in Example 2,
"xmlStruct.book.author" is an array.

The following function can be used to always ensure that specified nodes are
always stored as an array.

<cfscript>
function ForceArray (vKey) {
 if (NOT IsArray(Evaluate(vKey))) {
  tmp = ArrayNew(1);
  tmp[1] = Duplicate(Evaluate(vKey));
  StructDelete(Evaluate(Reverse(ListRest(Reverse(vKey), "."))),
ListLast(vKey, "."));
  SetVariable(vKey, Duplicate(tmp));
 }
}
</cfscript>


Put a <cftry> block around each function call in case no specified nodes
exist:

<cftry><cfset x =
ForceArray("xmlStruct.Profiles.Profile")><cfcatch></cfcatch></cftry>

I've only tested this on CF5 on WindowsNT.






-----------------------+
cf-xml mailing list
http://torchbox.com/xml/list.cfm

Reply via email to