Avg. Rating 5.0

Problem

When producing xml for AJAX projects, CFXML + CFOUTPUT is too slow.

Solution

Using ColdFusion's xmlelemnew and xmlnew function to create xml objects.

Detailed explanation

Using cfscript to write a ffunction and convert a query to a xml variable.

<cfscript>
  function queryToXML(qry){
           xmlobject=xmlnew();
           xmlobject.xmlRoot =XmlElemNew(xmlobject,"info");
           nodeAttributes=structnew();
          
structinsert(nodeAttributes,'recordcount',qry.recordcount);
           xmlobject.xmlRoot.xmlattributes=nodeAttributes;
           columnList=lcase(qry.columnlist);
           for(i=1;i lte qry.recordcount;i=i+1){
                  xmlNode=XmlElemNew(xmlobject,"item");
                  nodeAttributes=structnew();
                  for(ii=1;ii ltelistlen(columnList,',');ii=ii+1){
                          columnName=listgetat(columnList,ii,','); 
                         
StructInsert(nodeAttributes,columnName,urldecode(qry[columnName][i],'utf-8'))
                          }
                  xmlNode.xmlattributes=nodeAttributes;
                 
xmlobject.xmlRoot.xmlchildren[arraylen(xmlobject.xmlRoot.xmlchildren)+1]=xmlNode;
                }
            return xmlobject;
            }
</cfscript>

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes