xmlToQuery()

xmlToQuery()

Version: 1.2 build 20050824
Requires: Adobe ColdFusion MX 6.0 or greater
Total size: 1.98 KB
Download time: 0.35 seconds at 56kbps
Edition: Freeware

Description

This function takes an XML document and returns a query record set. You can then manipulate and display the record set using standard ColdFusion query functions and tags.

To build the record set, you must have a parsed document (see xmlParse()) and an XPath that defines the location of the node set that corresponds to the rows. For example, if you have a root node called records, and this node contains several nodes called record, then your XPath will be: path="/records/record/" .

The columnDataList is a list of data defining the columns for your record set. Each item in the list is a column name followed by an equals sign then a path in the document. Due to limitations in the CFMX support for XPath, XPaths are not used here. Instead you must write the path using structure / array notation. For example, if your record node has the attribute "name", then you would write: name=XmlAttributes.name . If your record node has the child node "details", then you would write: details=details.XmlText .

A full example of this function might read: xmlToQuery(xmlDoc, "/records/record/", "name=XmlAttributes.name,details=details.XmlText")

Returns

A string.

Category

Parameters


Parameter Type Required? Default Description
xmlDoc xmlDoc Yes document object
path xpath Yes An XPath leading to the node set that will form the rows of the query.
columnDataList string Yes Comma-delimited list of column data.

UDF source


 
<cfscript>
  /**
  * Convert an  document into a query record set
  * 
  * @param xmlDoc            document object (Required)
  * @param path             An XPath leading to the  node set that will form the rows of the query (Required)
  * @param columnDataList   Comma-delimited list of column data  (Required)
  * @return query record set
  * @author Matthew Walker, WWW.eswsoftware.com
  * @version 1.2, 2005-08-24 handles empty record fields - update by Nathanael Waite
  * @version 1.1, 2004-09-05 added querySetCell() - thanks Dan
  * @version 1, 2004-04-07
  */

  function xmlToQuery(xmlDoc, path, columnDataList) {
    var records = xmlSearch(xmlDoc, path);
    var i = 0;
    var j = 0;
    var temp = listToArray(columnDataList);
    var columns = arrayNew(1);
    var column = "";
    var columnList = "";
    var result = "";

    if ( not structKeyExists(request, "eswsoftware") )
      request.eswsoftware = structNew();
    if ( not structKeyExists(request.eswsoftware, "xmlToQuery") )
      request.eswsoftware.xmlToQuery = structNew();  
    request.eswsoftware.xmlToQuery.version = 1.2;
	    
    for ( i = 1; i lte arrayLen(temp); i = i + 1 ) {
      column = structNew();
      column.name = listFirst(temp[i], "=");
      column.path = listRest(temp[i], "=");
      arrayAppend(columns, column);
      columnList = listAppend(columnList, column.name);
    }
    result = queryNew(columnList);
	
    if ( arraylen(records) ) {
      queryAddRow(result, arrayLen(records));
      for ( i = 1; i lte arrayLen(records); i = i + 1 ) {
        record = records[i];
        for ( j = 1; j lte arrayLen(columns); j = j + 1 ) {
          column = columns[j];
          if ( structKeyExists(record, column.path) ) { 
            querySetCell(result, column.name, record[column.path]), i);
          }
          else {
            querySetCell(result, column.name, "", i);
          }
        }
      }
    }
	
    return result;
  }
  
</cfscript>
eXtensible Markup Language
World Wide Web
Macromedia ColdFusion MX
ColdFusion web application server version 6 or later.