/examples/api_jsonxml/resources/AnythingToXML/ArrayToXML.cfc
ColdFusion CFScript | 74 lines | 66 code | 8 blank | 0 comment | 2 complexity | f69bc1d8e91c09f9108bebf0f44a45a3 MD5 | raw file
1<cfcomponent namespace="ArrayToXML" displayname="ArrayToXML" output="no" > 2 <!--- Array to XML by Daniel Gaspar <daniel.gaspar@gmail.com> 5/1/2008 ---> 3 <!--- 4 5 Copyright 2008 Daniel Gaspar Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. You may obtain a copy of the 7 License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or 8 agreed to in writing, software distributed under the License is distributed on an "AS IS" 9 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License 10 for the specific language governing permissions and limitations under the License. 11 12 13 ---> 14 <cffunction name="init" access="public" output="no" returntype="any"> 15 <cfargument name="Include_Type_Hinting" type="numeric" required="no" default="1" /> 16 <cfargument name="XMLutils" type="any" required="yes" /> 17 <cfargument name="TabUtils" type="any" required="yes" /> 18 <cfset variables.Include_Type_Hinting = arguments.Include_Type_Hinting /> 19 <cfset variables.XMLutils = arguments.XMLutils /> 20 <cfset variables.TabUtils = arguments.TabUtils /> 21 <cfreturn this> 22 </cffunction> 23 24 <cffunction name="setAnythingToXML" access="public" output="no" returntype="void"> 25 <cfargument name="AnythingToXML" type="any" required="yes" /> 26 <cfset variables.AnythingToXML = arguments.AnythingToXML /> 27 </cffunction> 28 29 <cffunction name="ArrayToXML" access="public" output="no" returntype="string" > 30 <cfargument name="TheseItems" type="array" required="yes"> 31 <cfargument name="rootNodeName" type="string" required="no" default=""> 32 <cfargument name="AttributeList" type="string" required="no" default=""> 33 <cfset var xmlString = "" /> 34 <cfset var i = 1 /> 35 <cfprocessingdirective suppresswhitespace="yes"> 36 <cfsetting enablecfoutputonly="yes"> 37 <cfsavecontent variable="xmlString" > 38 <cfoutput>#variables.TabUtils.printtabs()#<#variables.XMLutils.getNodePlural(arguments.rootNodeName)# <cfif variables.Include_Type_Hinting eq 1>CF_TYPE='array'</cfif>></cfoutput> 39 <cfoutput>#createXML(arguments.TheseItems,arguments.rootNodeName,arguments.AttributeList)#</cfoutput> 40 <cfoutput>#variables.TabUtils.printtabs()#</#variables.XMLutils.getNodePlural(arguments.rootNodeName)#></cfoutput> 41 </cfsavecontent> 42 </cfprocessingdirective> 43 <cfreturn xmlString /> 44 </cffunction> 45 46 47 <cffunction name="createXML" access="public" output="no" returntype="string"> 48 <cfargument name="thisArray" type="Array" required="yes"> 49 <cfargument name="rootNodeName" type="string" required="no" default=""> 50 <cfargument name="AttributeList" type="string" required="no" default=""> 51 <cfset var thisSize = thisArray.size() /> 52 <cfset var xmlString = "" /> 53 <cfset var i = 1 /> 54 <cfset var CurrentNode = '' /> 55 <cfset variables.TabUtils.addtab() /> 56 <cfprocessingdirective suppresswhitespace="yes"> 57 <cfsetting enablecfoutputonly="yes"> 58 <cfsavecontent variable="xmlString" > 59 <cfloop from="1" to="#thisSize#" index="i" > 60 <cfif IsSimpleValue(thisArray[i])> 61 <cfset CurrentNode = variables.XMLutils.NodeNameCheck(rootNodeName) /> 62 <cfoutput>#variables.TabUtils.printtabs()#<#CurrentNode#><![CDATA[#trim(thisArray[i])#]]></#CurrentNode#></cfoutput> 63 <cfelse> 64 <!--- Yay for Recursion!---> 65 <cfoutput>#variables.AnythingToXML.ToXML(thisArray[i],arguments.rootNodeName,arguments.AttributeList)#</cfoutput> 66 </cfif> 67 </cfloop> 68 </cfsavecontent> 69 </cfprocessingdirective> 70 <cfset variables.TabUtils.removetab() /> 71 <cfreturn xmlString /> 72 </cffunction> 73 74</cfcomponent>