CFLib.org – Common Function Library Project

xmlExtractList(inString, tagName[, delimiter])

Last updated January 13, 2004

author

Samuel Neff

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
This UDF searches for all elements matching the specified tag name and returns a list from the text within those elements.

Return Values:
Returns a string.

Example:

<cfxml variable="EntryXML">
<users>
 <salesman>
  <systemname>user</systemname>
  <fullname>Mike Smith</fullname>
  <email>MSmith@noDomain.com</email>
 </salesman>
 <salesman>
  <systemname>user</systemname>
  <fullname>mark Hendrix Smith</fullname>
  <email>MHendrix@noDomain.com</email>
 </salesman>
 <salesman>
  <systemname>user</systemname>
  <fullname>Tammy Cochran</fullname>
  <email>MCochran@noDomain.com</email>
 </salesman>
 <salesman>
  <systemname>user</systemname>
  <fullname>Mike Smith</fullname>
  <email>MSmith@noDomain.com</email>
 </salesman>
</users>
</cfxml>

<cfoutput>#xmlExtractList(EntryXML, "email")#</cfoutput>

Parameters:

Name Description Required
inString Either an XML object or a string representation. Yes
tagName Tag to look for. Yes
delimiter Delimiter for returned string. Defaults to a comma. No

Full UDF Source:

<!---
 Extracts the text of named XML elements and returns it in a list.
 
 @param inString      Either an XML object or a string representation. (Required)
 @param tagName      Tag to look for. (Required)
 @param delimiter      Delimiter for returned string. Defaults to a comma. (Optional)
 @return Returns a string. 
 @author Samuel Neff (sam@serndesign.com) 
 @version 1, March 16, 2004 
--->
<cffunction name="xmlExtractList" returnType="string" output="no">
   <cfargument name="inString" type="any">
   <cfargument name="tagName" type="string">
   <cfargument name="delim" default=",">
   
   <cfset var inXML = "">
   
   <cfset var elementsArray = "">
   <cfset var valuesArray = arrayNew(1)>
   <cfset var i=1>
   <cfset var j=1>
   <cfset var ret = "">
   
   <cfif isXmlDoc(arguments.inString)>
      <cfset inXML = arguments.inString>
   <cfelse>
      <cfset inXML  = xmlParse(arguments.inString)>
   </cfif>
   
   <cfset elementsArray = xmlSearch(inXML, "//" & arguments.tagName)>

   
   <cfloop index="j" from="1" to="#arrayLen(elementsArray)#">
      <cfif elementsArray[j].xmlText neq "">
         <cfset valuesArray[i] = elementsArray[j].xmlText>
         <cfset i=i+1>
      </cfif>
   </cfloop>
   
   <cfset ret = arrayToList(valuesArray, arguments.delim)>
   <cfreturn ret>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson