| 1 | <!--- |
|---|
| 2 | Name : toxml |
|---|
| 3 | Author : Raymond Camden (ray@camdenfamily.com) |
|---|
| 4 | Created : 2006 |
|---|
| 5 | Last Updated : 5/12/07 |
|---|
| 6 | History : Switched to stringbuffer (rkc 7/13/06) |
|---|
| 7 | : xmlFormat doesn't strip out extended MS chars (rkc 11/2/06) |
|---|
| 8 | : strip another char (rkc 5/12/07) |
|---|
| 9 | ---> |
|---|
| 10 | <cfcomponent displayName="To XML" hint="Set of utility functions to generate XML" output="false"> |
|---|
| 11 | |
|---|
| 12 | <cffunction name="arrayToXML" returnType="string" access="public" output="false" hint="Converts an array into XML"> |
|---|
| 13 | <cfargument name="data" type="array" required="true"> |
|---|
| 14 | <cfargument name="rootelement" type="string" required="true"> |
|---|
| 15 | <cfargument name="itemelement" type="string" required="true"> |
|---|
| 16 | |
|---|
| 17 | <cfset var s = createObject('java','java.lang.StringBuffer').init("<?xml version=""1.0"" encoding=""UTF-8""?>")> |
|---|
| 18 | <cfset var x = ""> |
|---|
| 19 | |
|---|
| 20 | <cfset s.append("<#arguments.rootelement#>")> |
|---|
| 21 | |
|---|
| 22 | <cfloop index="x" from="1" to="#arrayLen(arguments.data)#"> |
|---|
| 23 | <cfset s.append("<#arguments.itemelement#>#xmlFormatBetter(arguments.data[x])#</#arguments.itemelement#>")> |
|---|
| 24 | </cfloop> |
|---|
| 25 | |
|---|
| 26 | <cfset s.append("</#arguments.rootelement#>")> |
|---|
| 27 | |
|---|
| 28 | <cfreturn s.toString()> |
|---|
| 29 | </cffunction> |
|---|
| 30 | |
|---|
| 31 | <cffunction name="listToXML" returnType="string" access="public" output="false" hint="Converts a list into XML."> |
|---|
| 32 | <cfargument name="data" type="string" required="true"> |
|---|
| 33 | <cfargument name="rootelement" type="string" required="true"> |
|---|
| 34 | <cfargument name="itemelement" type="string" required="true"> |
|---|
| 35 | <cfargument name="delimiter" type="string" required="false" default=","> |
|---|
| 36 | |
|---|
| 37 | <cfreturn arrayToXML( listToArray(arguments.data, arguments.delimiter), arguments.rootelement, arguments.itemelement)> |
|---|
| 38 | </cffunction> |
|---|
| 39 | |
|---|
| 40 | <cffunction name="queryToXML" returnType="string" access="public" output="false" hint="Converts a query to XML"> |
|---|
| 41 | <cfargument name="data" type="query" required="true"> |
|---|
| 42 | <cfargument name="rootelement" type="string" required="true"> |
|---|
| 43 | <cfargument name="itemelement" type="string" required="true"> |
|---|
| 44 | <cfargument name="cDataCols" type="string" required="false" default=""> |
|---|
| 45 | |
|---|
| 46 | <cfset var s = createObject('java','java.lang.StringBuffer').init("<?xml version=""1.0"" encoding=""UTF-8""?>")> |
|---|
| 47 | <cfset var col = ""> |
|---|
| 48 | <cfset var columns = arguments.data.columnlist> |
|---|
| 49 | <cfset var txt = ""> |
|---|
| 50 | |
|---|
| 51 | <cfset s.append("<#arguments.rootelement#>")> |
|---|
| 52 | |
|---|
| 53 | <cfloop query="arguments.data"> |
|---|
| 54 | <cfset s.append("<#arguments.itemelement#>")> |
|---|
| 55 | |
|---|
| 56 | <cfloop index="col" list="#columns#"> |
|---|
| 57 | <cfset txt = arguments.data[col][currentRow]> |
|---|
| 58 | <cfif isSimpleValue(txt)> |
|---|
| 59 | <cfif listFindNoCase(arguments.cDataCols, col)> |
|---|
| 60 | <cfset txt = "<![CDATA[" & txt & "]]" & ">"> |
|---|
| 61 | <cfelse> |
|---|
| 62 | <cfset txt = xmlFormatBetter(txt)> |
|---|
| 63 | </cfif> |
|---|
| 64 | <cfelse> |
|---|
| 65 | <cfset txt = ""> |
|---|
| 66 | </cfif> |
|---|
| 67 | |
|---|
| 68 | <cfset s.append("<#col#>#txt#</#col#>")> |
|---|
| 69 | |
|---|
| 70 | </cfloop> |
|---|
| 71 | |
|---|
| 72 | <cfset s.append("</#arguments.itemelement#>")> |
|---|
| 73 | </cfloop> |
|---|
| 74 | |
|---|
| 75 | <cfset s.append("</#arguments.rootelement#>")> |
|---|
| 76 | |
|---|
| 77 | <cfreturn s.toString()> |
|---|
| 78 | </cffunction> |
|---|
| 79 | |
|---|
| 80 | <cffunction name="structToXML" returnType="string" access="public" output="false" hint="Converts a struct into XML."> |
|---|
| 81 | <cfargument name="data" type="struct" required="true"> |
|---|
| 82 | <cfargument name="rootelement" type="string" required="true"> |
|---|
| 83 | <cfargument name="itemelement" type="string" required="true"> |
|---|
| 84 | <cfset var s = createObject('java','java.lang.StringBuffer').init("<?xml version=""1.0"" encoding=""UTF-8""?>")> |
|---|
| 85 | |
|---|
| 86 | <cfset var keys = structKeyList(arguments.data)> |
|---|
| 87 | <cfset var key = ""> |
|---|
| 88 | |
|---|
| 89 | <cfset s.append("<#arguments.rootelement#>")> |
|---|
| 90 | <cfset s.append("<#arguments.itemelement#>")> |
|---|
| 91 | |
|---|
| 92 | <cfloop index="key" list="#keys#"> |
|---|
| 93 | <cfset s.append("<#key#>#xmlFormatBetter(arguments.data[key])#</#key#>")> |
|---|
| 94 | </cfloop> |
|---|
| 95 | |
|---|
| 96 | <cfset s.append("</#arguments.itemelement#>")> |
|---|
| 97 | <cfset s.append("</#arguments.rootelement#>")> |
|---|
| 98 | |
|---|
| 99 | <cfreturn s.toString()> |
|---|
| 100 | </cffunction> |
|---|
| 101 | |
|---|
| 102 | <!--- Based on safetext from Nathan Dintenfas ---> |
|---|
| 103 | <cffunction name="xmlFormatBetter" returnType="string" access="public" output="false" hint="A real working XMLFormat."> |
|---|
| 104 | <cfargument name="txt" type="string" required="true"> |
|---|
| 105 | |
|---|
| 106 | <!--- Fix damn smart quotes. Thank you Microsoft! ---> |
|---|
| 107 | <!--- This line taken from Nathan Dintenfas' SafeText UDF ---> |
|---|
| 108 | <!--- www.cflib.org/udf.cfm/safetext ---> |
|---|
| 109 | <cfset arguments.txt = replaceList(arguments.txt,chr(8216) & "," & chr(8217) & "," & chr(8220) & "," & chr(8221) & "," & chr(8212) & "," & chr(8213) & "," & chr(8230) & "," & chr(25),"',',"","",--,--,..., ")> |
|---|
| 110 | <!--- stuff to remove ---> |
|---|
| 111 | <cfset arguments.txt = replace(arguments.txt, chr(8211), "", "all")> |
|---|
| 112 | |
|---|
| 113 | <cfreturn xmlFormat(arguments.txt)> |
|---|
| 114 | </cffunction> |
|---|
| 115 | |
|---|
| 116 | </cfcomponent> |
|---|