Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iknowkungfoo/683d3bc9dfce2595146aa4977eef8c68 to your computer and use it in GitHub Desktop.
Save iknowkungfoo/683d3bc9dfce2595146aa4977eef8c68 to your computer and use it in GitHub Desktop.
ColdFusion function to convert an array of structs to proper XML.
<cffunction name="arrayOfStructsToXML" access="public" output="false" returntype="any">
<cfargument name="data" type="array" required="true">
<cfscript>
var buffer = createObject("java","java.lang.StringBuilder").init('');
var y = arrayLen(arguments.data);
var xData = "";
buffer.append('<games>');
for (local.x = 1; local.x <= local.y; local.x++) {
local.stGame = arguments.data[local.x];
buffer.append('<game>');
for (key in local.stGame) {
buffer.append('<' & lcase(key) & '>');
if (!isDate(local.stGame[key]) && !isNumeric(local.stGame[key])) {
buffer.append(encodeForXML(local.stGame[key]));
} else {
buffer.append(local.stGame[key]);
}
buffer.append('</' & lcase(key) & '>');
}
buffer.append('</game>');
}
buffer.append('</games>');
xData = xmlParse(buffer.toString());
if (isXML(xData)) {
return (xData);
} else {
return "";
}
</cfscript>
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment