Guest User

Anuj Gakhar

a guest
Mar 10th, 2008
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <cffunction name="ConvertXmlToStruct" access="public" returntype="struct" output="false"
  2.                 hint="Parse raw XML response body into ColdFusion structs and arrays and return it.">
  3.     <cfargument name="xmlNode" type="string" required="true" />
  4.     <cfargument name="str" type="struct" required="true" />
  5.     <!---Setup local variables for recurse: --->
  6.     <cfset var i = 0 />
  7.     <cfset var axml = arguments.xmlNode />
  8.     <cfset var astr = arguments.str />
  9.     <cfset var n = "" />
  10.     <cfset var tmpContainer = "" />
  11.    
  12.     <cfset axml = XmlSearch(XmlParse(arguments.xmlNode),"/node()")>
  13.     <cfset axml = axml[1] />
  14.     <!--- For each children of context node: --->
  15.     <cfloop from="1" to="#arrayLen(axml.XmlChildren)#" index="i">
  16.         <!--- Read XML node name without namespace: --->
  17.         <cfset n = replace(axml.XmlChildren[i].XmlName, axml.XmlChildren[i].XmlNsPrefix&":", "") />
  18.         <!--- If key with that name exists within output struct ... --->
  19.         <cfif structKeyExists(astr, n)>
  20.             <!--- ... and is not an array... --->
  21.             <cfif not isArray(astr[n])>
  22.                 <!--- ... get this item into temp variable, ... --->
  23.                 <cfset tmpContainer = astr[n] />
  24.                 <!--- ... setup array for this item beacuse we have multiple items with same name, ... --->
  25.                 <cfset astr[n] = arrayNew(1) />
  26.                 <!--- ... and reassing temp item as a first element of new array: --->
  27.                 <cfset astr[n][1] = tmpContainer />
  28.             <cfelse>
  29.                 <!--- Item is already an array: --->
  30.                
  31.             </cfif>
  32.             <cfif arrayLen(axml.XmlChildren[i].XmlChildren) gt 0>
  33.                     <!--- recurse call: get complex item: --->
  34.                     <cfset astr[n][arrayLen(astr[n])+1] = ConvertXmlToStruct(axml.XmlChildren[i], structNew()) />
  35.             <cfelse>
  36.                     <!--- else: assign node value as last element of array: --->
  37.                     <cfset astr[n][arrayLen(astr[n])+1] = axml.XmlChildren[i].XmlText />
  38.             </cfif>
  39.         <cfelse>
  40.             <!---
  41.                 This is not a struct. This may be first tag with some name.
  42.                 This may also be one and only tag with this name.
  43.             --->
  44.             <!---
  45.                     If context child node has child nodes (which means it will be complex type): --->
  46.             <cfif arrayLen(axml.XmlChildren[i].XmlChildren) gt 0>
  47.                 <!--- recurse call: get complex item: --->
  48.                 <cfset astr[n] = ConvertXmlToStruct(axml.XmlChildren[i], structNew()) />
  49.             <cfelse>
  50.                 <cfif IsStruct(aXml.XmlAttributes) AND StructCount(aXml.XmlAttributes)>
  51.                     <cfset at_list = StructKeyList(aXml.XmlAttributes)>
  52.                     <cfloop from="1" to="#listLen(at_list)#" index="atr">
  53.                          <cfif ListgetAt(at_list,atr) CONTAINS "xmlns:">
  54.                              <!--- remove any namespace attributes--->
  55.                             <cfset Structdelete(axml.XmlAttributes, listgetAt(at_list,atr))>
  56.                          </cfif>
  57.                      </cfloop>
  58.                      <!--- if there are any atributes left, append them to the response--->
  59.                      <cfif StructCount(axml.XmlAttributes) GT 0>
  60.                          <cfset astr['_attributes'] = axml.XmlAttributes />
  61.                     </cfif>
  62.                 </cfif>
  63.                 <!--- else: assign node value as last element of array: --->
  64.                 <!--- if there are any attributes on this element--->
  65.                 <cfif IsStruct(aXml.XmlChildren[i].XmlAttributes) AND StructCount(aXml.XmlChildren[i].XmlAttributes) GT 0>
  66.                     <!--- assign the text --->
  67.                     <cfset astr[n] = axml.XmlChildren[i].XmlText />
  68.                         <!--- check if there are no attributes with xmlns: , we dont want namespaces to be in the response--->
  69.                      <cfset attrib_list = StructKeylist(axml.XmlChildren[i].XmlAttributes) />
  70.                      <cfloop from="1" to="#listLen(attrib_list)#" index="attrib">
  71.                          <cfif ListgetAt(attrib_list,attrib) CONTAINS "xmlns:">
  72.                              <!--- remove any namespace attributes--->
  73.                             <cfset Structdelete(axml.XmlChildren[i].XmlAttributes, listgetAt(attrib_list,attrib))>
  74.                          </cfif>
  75.                      </cfloop>
  76.                      <!--- if there are any atributes left, append them to the response--->
  77.                      <cfif StructCount(axml.XmlChildren[i].XmlAttributes) GT 0>
  78.                          <cfset astr[n&'_attributes'] = axml.XmlChildren[i].XmlAttributes />
  79.                     </cfif>
  80.                 <cfelse>
  81.                      <cfset astr[n] = axml.XmlChildren[i].XmlText />
  82.                 </cfif>
  83.             </cfif>
  84.         </cfif>
  85.     </cfloop>
  86.     <!--- return struct: --->
  87.     <cfreturn astr />
  88. </cffunction>
RAW Paste Data