Created
June 6, 2019 15:43
-
-
Save iknowkungfoo/683d3bc9dfce2595146aa4977eef8c68 to your computer and use it in GitHub Desktop.
ColdFusion function to convert an array of structs to proper XML.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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