/examples/api_jsonxml/resources/AnythingToXML/ArrayToXML.cfc

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