CFLib.org – Common Function Library Project

GetXmlAttribute(node, attribute[, default])

Last updated December 23, 2002

author

Kreig Zimmerman

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
This tag will allow you to retrieve an XML attribute from an XML node. Furthermore, if you are in the habit of needing default values in case the attribute's value is empty or the attribute does not exist, you can specify that as well.

Return Values:
Returns a string.

Example:

<cfxml variable="XmlScheme">
<cfoutput>
<xmlscheme>
	<xmlnode oneatt="1" threeatt="3">Some Content</xmlnode>
</xmlscheme>
</cfoutput>
</cfxml>
<cfdump var="#XmlScheme#">
<cfscript>
	oneVar=GetXmlAttribute(XmlScheme.XmlRoot.XmlChildren[1],"oneatt");
	WriteOutput(oneVar);
	twoVar=GetXmlAttribute(XmlScheme.XmlRoot.XmlChildren[1],"twoatt",0);
	WriteOutput(twoVar);
</cfscript>

Parameters:

Name Description Required
node XML note to retrieve the attribute from. Yes
attribute Attribute to retrieve. Yes
default If attribute does not exist, return this default. No

Full UDF Source:

<!---
 Pass in an XML Node and attribute reference to receive the attribute's value.
 
 @param node 	 XML note to retrieve the attribute from. (Required)
 @param attribute 	 Attribute to retrieve. (Required)
 @param default 	 If attribute does not exist, return this default. (Optional)
 @return Returns a string. 
 @author Kreig Zimmerman (kkz@foureyes.com) 
 @version 1, December 23, 2002 
--->
<cffunction name="GetXmlAttribute" output="false" returntype="any">
	<cfargument name="node" type="any" required="yes">
	<cfargument name="attribute" type="string" required="Yes">
	<cfargument name="default" type="string" default="" required="false">
	<cfset var myResult="#arguments.default#">
	<cfif StructKeyExists(node.XmlAttributes, attribute)>
		<cfset myResult=node.XmlAttributes["#attribute#"]>
	</cfif>
	<cfreturn myResult>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson