XML Functions

Contact Us or call 1-877-932-8228
XML Functions

XML Functions

ColdFusion provides many XML functions for dealing with XML documents. This section covers those functions and their functionality.

Creating New XML Documents

There are several ways to create new XML documents, which ColdFusion treats as structures. The root element of an XML document can be accessed as follows: xmlDoc.xmlRoot; or by the name of the root element: xmlDoc.rootName, where the root element is <rootName/>.

XmlParse()

The XmlParse() function is used to convert an XML string into an XML document object, which makes the XML searchable through other ColdFusion XML functions. The function takes the following parameters:

XmlParse() Parameters
Parameter Description
xmlText Required. Can be any of the following:
  • An XML string
  • A absolute path or URL to an XML file
caseSensitive Optional boolean. If true, the object's properties (elements and attributes) will be case sensitive. The default is false. When an object is case sensitive, you must use bracket notation to reference elements and attributes.
validator Optional. Can be either of the following:
  • A string containing a DTD or XML schema
  • An absolute path or URL to a DTD or XML schema

The following sample shows how it is used.

Code Sample:

XML/Demos/XmlParse.cfm
<html>
<head>
<title>XmlParse()</title>
</head>
<body>
<h1>XmlParse()</h1>
<cfset xmlGeorge=XMLParse(ExpandPath("George.xml"))>
<cfoutput>
	<h2>#xmlGeorge.Name.XmlAttributes.Title# #xmlGeorge.Name.FirstName# #xmlGeorge.Name.LastName#</h2>
</cfoutput>
<cfdump var="#xmlGeorge#">
</body>
</html>

In a browser, the page looks like this:

As you can see, the XML document's properties can be accessed like a ColdFusion structure. Elements can be accessed as direct properties of their parent element. Attributes must be accessed through the XmlAttributes structure.

XmlNew(), XmlElemNew(), xmlRoot, XmlChildren, XmlAttributes

New XML documents can also be built from scratch using the XmlNew() function. Elements are added with the XmlElemNew() function, which requires the XML document object of which the element will be a part and the name of the new element. The root element should be assigned to the xmlRoot property of the XML document. Elements and attributes are positioned in the document with XmlChildren and XmlAttributes. The following document shows how to build an XML document from scratch.

Code Sample:

XML/Demos/XmlNew.cfm
<!---Create the XML document--->
<cfset docGeorge=XmlNew()>

<!---Set the root element--->
<cfset docGeorge.xmlRoot = XmlElemNew(docGeorge,"Name")>

<!---Set an attribute of the root element--->
<cfset docGeorge.xmlRoot.XmlAttributes.Title = "President">

<!---Create the FirstName and LastName elements--->
<cfset elemFirstName = XmlElemNew(docGeorge,"FirstName")>
<cfset elemLastName = XmlElemNew(docGeorge,"LastName")>

<!---Set the text values of the FirstName and LastName elements--->
<cfset elemFirstName.XmlText = "George">
<cfset elemLastName.XmlText = "Washington">

<!---
	Append the FirstName and LastName elements to the root element
	Notice this can be done using xmlRoot or the name of the root element
--->
<cfset docGeorge.xmlRoot.XmlChildren[1] = elemFirstName>
<cfset docGeorge.Name.XmlChildren[2] = elemLastName>

<!---Convert the XML document to a string--->
<cfset xmlString = ToString(docGeorge)>

<!---Set the content to text/xml, reset the buffer, and output the XML string--->
<cfcontent type="text/xml" reset="yes"><cfoutput>#xmlString#</cfoutput>

In a browser, the page looks like this:

The <cfxml> Tag

The <cfxml> tag can be used to create an XML document object directly in XML. It tends to be easier than using XmlNew() if the XML structure is not determined programatically. The following example shows how to use <cfxml>.

Code Sample:

XML/Demos/cfxml.cfm
               <cfxml variable="docGeorge">
<?xml version="1.0" encoding="iso-8859-1"?>
<Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Name.xsd" Title="President">
	<FirstName>George</FirstName>
	<LastName>Washington</LastName>
</Name>
</cfxml>

<!---Convert the XML document to a string--->
<cfset xmlString = ToString(docGeorge)>

<!---Set the content to text/xml, reset the buffer, and output the XML string--->
<cfcontent type="text/xml" reset="yes"><cfoutput>#xmlString#</cfoutput>

XmlTransform()

The XmlTransform() function is used to transform XML documents against XSLT documents. It returns a string containing the result of the transformation. The function takes the following parameters:

XmlTransform() Parameters
Parameter Description
xml Required. Can be either of the following:
  • An XML string
  • An XML document object
xsl Required. Can be either of the following:
  • An XSLT string
  • An absolute path or URL to the XSLT
parameters Optional. A structure containing name-value pairs that map to the parameters defined in the XSLT. The properties of the structure must be added via the StructInsert() function.

To illustrate, we will use the following XML and XSLT documents:

Code Sample:

XML/Demos/George.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Name.xsd" Title="President">
	<FirstName>George</FirstName>
	<LastName>Washington</LastName>
</Name>

Code Sample:

XML/Demos/Name.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:param name="bgcolor" select="'white'"/>
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
			<html>
				<head>
					<title>
						<xsl:value-of select="Name/@Title"/>
						<xsl:text> </xsl:text>
						<xsl:value-of select="Name/FirstName"/>
						<xsl:text> </xsl:text>
						<xsl:value-of select="Name/LastName"/>
					</title>
				</head>
				<body bgcolor="{$bgcolor}">
					<h1>
						<xsl:value-of select="Name/@Title"/>
						<xsl:text> </xsl:text>
						<xsl:value-of select="Name/FirstName"/>
						<xsl:text> </xsl:text>
						<xsl:value-of select="Name/LastName"/></h1>
				</body>
			</html>
	</xsl:template>
</xsl:stylesheet>

Notice that Name.xsl can accept a bgcolor parameter, which it uses to set the background color of the page. The following ColdFusion file will transform George.xml against Name.xsl.

Code Sample:

XML/Demos/XmlTransform.cfm
<cffile action="read" variable="strXML" file="#ExpandPath('George.xml')#">
<cfset PathToXslt = ExpandPath("Name.xsl")>
<cfset params = StructNew()>
<cfset StructInsert(params,"bgcolor","silver")>
<cfoutput>#XmlTransform(strXML,PathToXslt,params)#</cfoutput>

Notice that we have to read the XML file into a string with <cffile> before passing it to XmlTransform(), which cannot accept a path to the XML file as the first argument. The resulting HTML code will look like this:

The page will display as follows:

XmlFormat()

The XmlFormat() function is used to escape special XML characters so that they can be displayed in a browser. For example, the < character becomes &lt;. The following example shows how we could display the code resulting from the transformation we just did in the browser window.

Code Sample:

XML/Demos/XmlFormat.cfm
<cffile action="read" variable="strXML" file="#ExpandPath('George.xml')#">
<cfset PathToXslt = ExpandPath("Name.xsl")>
<cfset params = StructNew()>
<cfset StructInsert(params,"bgcolor","silver")>
<cfset result = XmlTransform(strXML,PathToXslt,params)>

<cfset formattedXml = XmlFormat(result)>

<html>
<head>
<title>XmlFormat()</title>
</head>
<body>
<h1>XmlFormat()</h1>
<cfoutput>
<pre>#formattedXml#</pre>
</cfoutput>
</body>
</html>

The resulting page is shown below:

Searching XML Documents

XML documents can be searched using the XmlSearch() function. Specific elements can be located using the XmlChildPos() function.

XmlSearch()

The XmlSearch() function uses an XPath expression to search an XML document object. The function takes two arguments: the document object to search and the XPath expression. It returns an array of XML nodes that match the expression. The code samples below show how the function is used. The first sample is the XML document being searched. The second sample is the ColdFusion file that uses the XmlSearch() function.

Code Sample:

XML/Demos/Beatles.xml
<?xml version="1.0"?>
<beatles>
	<beatle link="http://www.paulmccartney.com">
		<name>
			<firstname>Paul</firstname>
			<lastname>McCartney</lastname>
		</name>
	</beatle>
	<beatle link="http://www.johnlennon.com">
		<name>
			<firstname>John</firstname>
			<lastname>Lennon</lastname>
		</name>
	</beatle>
	<beatle link="http://www.georgeharrison.com">
		<name>
			<firstname>George</firstname>
			<lastname>Harrison</lastname>
		</name>
	</beatle>
	<beatle link="http://www.ringostarr.com">
		<name>
			<firstname>Ringo</firstname>
			<lastname>Starr</lastname>
		</name>
	</beatle>
</beatles>

Code Sample:

XML/Demos/XmlSearch.cfm
<html>
<head>
<title>XmlSearch()</title>
</head>
<body>
<h1>XmlSearch()</h1>
<cfset xmlBeatles=XMLParse(ExpandPath("Beatles.xml"))>
<cfoutput>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
	<th>XPath</th>
	<th>Result</th>
</tr>
<cfset arrXPs = ArrayNew(1)>
<cfset ArrayAppend(arrXPs,"/beatles")>
<cfset ArrayAppend(arrXPs,"/beatles/beatle/@link")>
<cfset ArrayAppend(arrXPs,"/beatles//firstname")>
<cfset ArrayAppend(arrXPs,"/beatles//firstname/text()")>

<cfloop index="i" from="1" to="#ArrayLen(arrXPs)#">
	<cfset xp = arrXPs[i]>
	<cfset arrResult = XmlSearch(xmlBeatles,xp)>
	<tr valign="top">
		<td>#xp#</td>
		<td>
			<ol>
			<cfloop index="i" from="1" to="#ArrayLen(arrResult)#">
				<li>
					#XmlGetNodeType(arrResult[i])#: 
					<cftry>
						#arrResult[i].XmlText#
						<cfcatch type="any">#arrResult[i]#</cfcatch>
					</cftry>
				</li>
			</cfloop>
			</ol>
		</td>
	</tr>
</cfloop>
</table>
</cfoutput>
</body>
</html>

The code loops through a set of XPaths, uses each one to search the XML document and displays the results as a list. If the resulting node has an XmlText property (e.g, an element), that property is displayed. If it does not (e.g, an attribute), the node itself is displayed. The output is shown below:

XmlChildPos()

The XmlChildPos() function finds the position of a node within its parent element. This can be used for adding or removing elements to the XML DOM. The code samples below show how the function is used. The first sample is the XML document being searched. The second sample is the ColdFusion file that uses the XmlChildPos() function.

Code Sample:

XML/Demos/RockStars.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<rockstars>
	<beatle link="http://www.paulmccartney.com">
		<name>
			<firstname>Paul</firstname>
			<lastname>McCartney</lastname>
		</name>
	</beatle>
	<beatle link="http://www.johnlennon.com">
		<name>
			<firstname>John</firstname>
			<lastname>Lennon</lastname>
		</name>
	</beatle>
	<stone link="http://www.mickjagger.com" >
		<name>
			<firstname>Mick</firstname>
			<lastname>Jagger</lastname>
		</name>
	</stone>
	<stone link="http://www.keithrichards.com" >
		<name>
			<firstname>Keith</firstname>
			<lastname>Richards</lastname>
		</name>
	</stone>
	<beatle link="http://www.webucator.com">
		<name>
			<firstname>Nat</firstname>
			<lastname>Dunn</lastname>
		</name>
	</beatle>
</rockstars>

Code Sample:

XML/Demos/XmlChildPos.cfm
<cfset xmlRockStars=XMLParse(ExpandPath("RockStars.xml"))>
<cfset arrRockStars=xmlRockStars.rockstars.XmlChildren>
<html>
<head>
<title>XmlChildPos()</title>
</head>
<body>
<h1>XmlChildPos()</h1>
<cfoutput>
	<ol>
	<cfloop index="i" from="1" to="#ArrayLen(arrRockStars)#">
		<li>#arrRockStars[i]#</li>
	</cfloop>
	</ol>
	
	<cfset notARockStarPos = XmlChildPos(xmlRockStars.rockstars,"beatle",3)>
	
	<div style="color:red; font-size:larger;">
		The person at position #notARockStarPos# is not a rock star and will be deleted.
	</div>
	
	<cfset ArrayDeleteAt(arrRockStars,notARockStarPos)>
	
	<ol>
	<cfloop index="i" from="1" to="#ArrayLen(arrRockStars)#">
		<li>#arrRockStars[i]#</li>
	</cfloop>
	</ol>
</cfoutput>
</body>
</html>

The document displays all the XML children of the rockstars element. The XmlChildPos() function finds the third beatle element and identifies it as the fifth element within rockstars and uses that information to delete the element from the rockstars.XmlChildren array. It then redisplays the XML children of the rockstars element. Notice that the non-rockstar is missing:

Validating XML Documents

ColdFusion provides functions for checking if an XML document is well formed or valid against an XML schema or DTD. There are additional functions for checking different parts of an XML document. These functions are explained below.

IsXML()

The IsXML() function determines whether the passed-in string is well-formed XML. It returns true if it is and false if it is not. The following sample shows how it is used.

Code Sample:

XML/Demos/IsXML.cfm
<cfparam name="FORM.XmlText" default="<Name>
	<FirstName>George</FirstName>
	<LastName>Washington</LastName>
</Name>">
<html>
<head>
<title>IsXML()</title>
</head>
<body>
<h1>XML Test</h1>
<cfoutput>
<form method="post" action="#CGI.SCRIPT_NAME#">
<textarea name="XmlText" cols="60" rows="10">#FORM.XmlText#</textarea>
<br><input name="SubmitButton" type="submit" value="Check XML">
<cfif isDefined("FORM.SubmitButton")>
	<cfif IsXML(FORM.XmlText)>
		<span style="margin-left:200px;color:green">XML is well formed.</span>
	<cfelse>
		<span style="margin-left:200px;color:red">XML is not well formed.</span>
	</cfif>
</cfif>
</form>
</cfoutput>
</body>
</html>

The page simply displays a form with a textarea for holding XML. When the form is submitted, the page uses the IsXML() function to check if the text is well-formed XML and displays a message with the results.

When the page is first opened, it looks like this:

When the form is submitted with well-formed XML, the page displays as follows (notice the little message of success in the bottom right corner):

When the form is submitted with poorly-formed XML, the page displays as follows:

XmlValidate()

The XmlValidate() function is used to validate an XML document against a DTD or XML schema. The function takes the following parameters:

XmlValidate() Parameters
Parameter Description
xmlDoc Required. Can be any of the following:
  • An XML string
  • An absolute path or URL to an XML file
  • An XML document object
validator Optional. Can be either of the following:
  • A string containing a DTD or XML schema
  • An absolute path or URL to a DTD or XML schema

The function returns a structure containing the following fields:

  • Status - A boolean indicating if validation was successful.
  • Errors - An array of errors.
  • FatalErrors - An array of fatal errors, usually indicating that the XML document is not well formed.
  • Warnings - An array of warnings.

To illustrate, we will use the following simple XML schema:

Code Sample:

XML/Demos/Name.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Name">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="FirstName" type="xs:string"/>
				<xs:element name="LastName" type="xs:string"/>
			</xs:sequence>
			<xs:attribute name="Title" type="xs:string" use="optional"/>
		</xs:complexType>
	</xs:element>
</xs:schema>

The schema requires the document element Name to have two children, FirstName and LastName, and an optional attribute, Title.

ColdFusion uses the Xerces parser to validate XML documents. To validate correctly when the validator parameter is being used, the parser requires the xmlns:xsi and xsi:noNamespaceSchemaLocation attributes in the document element of the XML being validated. To illustrate, we will use a demo similar to the IsXML() demo.

Code Sample:

XML/Demos/XmlValidate.cfm
<cfparam name="FORM.XmlText" default="<Name xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation=''>
	<FirstName>George</FirstName>
	<LastName>Washington</LastName>
</Name>">
<html>
<head>
<title>XmlValidate()</title>
</head>
<body>
<h1>XML Validation Test</h1>
<cfoutput>
<form method="post" action="#CGI.SCRIPT_NAME#">
<textarea name="XmlText" cols="60" rows="10">#FORM.XmlText#</textarea>
<br><input name="SubmitButton" type="submit" value="Validate XML">
<cfif isDefined("FORM.SubmitButton")>
	<cfset structResults = XmlValidate(FORM.XmlText,ExpandPath("Name.xsd"))>
	<cfif structResults.status>
		<span style="margin-left:200px;color:green">XML is valid.</span>
	<cfelse>
		<span style="margin-left:200px;color:red">XML is not valid.</span>
		<div style="margin-top:10px">
		<cfif ArrayLen(structResults.Errors)>
			<b>Errors:</b>
			<ol style="margin-top:0px">
				<cfloop index="i" from="1" to="#ArrayLen(structResults.Errors)#">
					<li>#structResults.errors[i]#</li>
				</cfloop>
			</ol>
		</cfif>
		<cfif ArrayLen(structResults.FatalErrors)>
			<b>Fatal Errors:</b>
			<ol style="margin-top:0px">
				<cfloop index="i" from="1" to="#ArrayLen(structResults.FatalErrors)#">
					<li>#structResults.fatalErrors[i]#</li>
				</cfloop>
			</ol>
		</cfif>
		<cfif ArrayLen(structResults.Warnings)>
			<b>Warnings:</b>
			<ol style="margin-top:0px">
				<cfloop index="i" from="1" to="#ArrayLen(structResults.Warnings)#">
					<li>#structResults.warnings[i]#</li>
				</cfloop>
			</ol>
		</cfif>
		</div>
	</cfif>
</cfif>
</form>
</cfoutput>
</body>
</html>

When the page is first opened, it looks like this:

When the form is submitted with valid XML, the page displays as follows (notice the little message of success in the bottom right corner):

When the form is submitted with invalid XML, the page displays as follows:

The XML has several problems:

  • The errors are problems with the validity of the XML document according to the schema. In this case, the title attribute should be spelled with a capital "T" as "Title" and no MiddleName element is allowed.
  • The fatal errors are problems with the well-formedness of the XML document. In this case, the LastName element's close tag should have a capital "N".
  • There are no warnings, which are generally difficult to produce in a validating parser.

IsXmlDoc(), IsXmlRoot(), IsXmlElem(), IsXmlAttribute(), IsXmlNode()

  • The IsXmlDoc() function returns true if the passed-in parameter is an XML document object.
  • The IsXmlRoot() function returns true if the passed-in parameter is the root of an XML document object.
  • The IsXmlElem() function returns true if the passed-in parameter is an XML element.
  • The IsXmlAttribute() function returns true if the passed-in parameter is an XML attribute.
  • The IsXmlNode() function returns true if the passed-in parameter is an XML node.

The following file shows how these functions work.

Code Sample:

XML/Demos/XmlParts.cfm
<cfset xmlGeorge=XMLParse(ExpandPath("George.xml"))>
<html>
<head>
<title>XML Parts</title>
<style type="text/css">
.YES {background-color:#00cc00; font-weight:bold;}
.NO {color:#ff0000;}
</style>
</head>
<body>
<h1>XML Parts</h1>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
	<th>#</th>
	<th>XML Part</th>
	<th>IsXmlDoc()</th>
	<th>IsXmlRoot()</th>
	<th>IsXmlElem()</th>
	<th>IsXmlAttribute()</th>
	<th>IsXmlNode()</th>
</tr>
<cfoutput>
<cfset part = xmlGeorge>
<tr>
	<td>1</td>
	<td>xmlGeorge</td>
	<td class="#IsXmlDoc(part)#">#IsXmlDoc(part)#</td>
	<td class="#IsXmlRoot(part)#">#IsXmlRoot(part)#</td>
	<td class="#IsXmlElem(part)#">#IsXmlElem(part)#</td>
	<td class="#IsXmlAttribute(part)#">#IsXmlAttribute(part)#</td>
	<td class="#IsXmlNode(part)#">#IsXmlNode(part)#</td>
</tr>
<cfset part = xmlGeorge.Name>
<tr>
	<td>2</td>
	<td>xmlGeorge.Name</td>
	<td class="#IsXmlDoc(part)#">#IsXmlDoc(part)#</td>
	<td class="#IsXmlRoot(part)#">#IsXmlRoot(part)#</td>
	<td class="#IsXmlElem(part)#">#IsXmlElem(part)#</td>
	<td class="#IsXmlAttribute(part)#">#IsXmlAttribute(part)#</td>
	<td class="#IsXmlNode(part)#">#IsXmlNode(part)#</td>
</tr>
<cfset part = xmlGeorge.Name.XmlAttributes.Title>
<tr>
	<td>3</td>
	<td>xmlGeorge.Name.XmlAttributes.Title</td>
	<td class="#IsXmlDoc(part)#">#IsXmlDoc(part)#</td>
	<td class="#IsXmlRoot(part)#">#IsXmlRoot(part)#</td>
	<td class="#IsXmlElem(part)#">#IsXmlElem(part)#</td>
	<td class="#IsXmlAttribute(part)#">#IsXmlAttribute(part)#</td>
	<td class="#IsXmlNode(part)#">#IsXmlNode(part)#</td>
</tr>
<cfset part = xmlGeorge.Name.FirstName>
<tr>
	<td>4</td>
	<td>xmlGeorge.Name.FirstName</td>
	<td class="#IsXmlDoc(part)#">#IsXmlDoc(part)#</td>
	<td class="#IsXmlRoot(part)#">#IsXmlRoot(part)#</td>
	<td class="#IsXmlElem(part)#">#IsXmlElem(part)#</td>
	<td class="#IsXmlAttribute(part)#">#IsXmlAttribute(part)#</td>
	<td class="#IsXmlNode(part)#">#IsXmlNode(part)#</td>
</tr>
<cfset part = xmlGeorge.Name.XmlChildren[1]>
<tr>
	<td>5</td>
	<td>xmlGeorge.Name.XmlChildren[1]</td>
	<td class="#IsXmlDoc(part)#">#IsXmlDoc(part)#</td>
	<td class="#IsXmlRoot(part)#">#IsXmlRoot(part)#</td>
	<td class="#IsXmlElem(part)#">#IsXmlElem(part)#</td>
	<td class="#IsXmlAttribute(part)#">#IsXmlAttribute(part)#</td>
	<td class="#IsXmlNode(part)#">#IsXmlNode(part)#</td>
</tr>
<cfset part = xmlGeorge.Name.FirstName.XmlText>
<tr>
	<td>6</td>
	<td>xmlGeorge.Name.FirstName.XmlText</td>
	<td class="#IsXmlDoc(part)#">#IsXmlDoc(part)#</td>
	<td class="#IsXmlRoot(part)#">#IsXmlRoot(part)#</td>
	<td class="#IsXmlElem(part)#">#IsXmlElem(part)#</td>
	<td class="#IsXmlAttribute(part)#">#IsXmlAttribute(part)#</td>
	<td class="#IsXmlNode(part)#">#IsXmlNode(part)#</td>
</tr>
</table>
</cfoutput>
</body>
</html>

The page will display as follows:

This is pretty straightforward. The only surprises are the way attributes and text nodes are handled. According to row 3, the Title attribute is not an XML attribute, nor is it an XML node. And according to row 6, the text inside of the FirstName element is not an XML node. The Title attribute is, of course, an XML attribute, and all attributes and text in an XML document are XML nodes; however, ColdFusion treats attributes as properties of an element, not as nodes, and it treats text as plain text, rather than as an XML text node.

XmlNodes Array

Every element has an XmlNodes array, which contains the element's element nodes, text nodes, comment nodes, and CDATA section nodes. It does not contain an element's attribute nodes. To access attributes, you must use the XmlAttributes structure.

XmlGetNodeType()

The XmlGetNodeType() function is used to determine the passed-in parameter's node type. The following two examples show how the function works.

Code Sample:

XML/Demos/GetNodeType.cfm
<cfset xmlGeorge=XMLParse(ExpandPath("George.xml"))>
<html>
<head>
<title>XmlGetNodeType()</title>
</head>
<body>
<h1>XmlGetNodeType()</h1>
<cfoutput>
<ol>
	<cfset part = xmlGeorge>
	<li>
		xmlGeorge: 
		<cftry>
			#XmlGetNodeType(part)#
			<cfcatch type="any"><span style="color:red">#cfcatch.Message#</span></cfcatch>
		</cftry>
	</li>
	
	<cfset part = xmlGeorge.Name>
	<li>
		xmlGeorge.Name: 
		<cftry>
			#XmlGetNodeType(part)#
			<cfcatch type="any"><span style="color:red">#cfcatch.Message#</span></cfcatch>
		</cftry>
	</li>
	
	<cfset part = xmlGeorge.Name.XmlAttributes.Title>
	<li>
		xmlGeorge.Name.XmlAttributes.Title: 
		<cftry>
			#XmlGetNodeType(part)#
			<cfcatch type="any"><span style="color:red">#cfcatch.Message#</span></cfcatch>
		</cftry>
	</li>
	
	<cfset part = xmlGeorge.Name.FirstName>
	<li>
		xmlGeorge.Name.FirstName: 
		<cftry>
			#XmlGetNodeType(part)#
			<cfcatch type="any"><span style="color:red">#cfcatch.Message#</span></cfcatch>
		</cftry>
	</li>
	
	<cfset part = xmlGeorge.Name.XmlChildren[1]>
	<li>
		xmlGeorge.Name.XmlChildren[1]: 
		<cftry>
			#XmlGetNodeType(part)#
			<cfcatch type="any"><span style="color:red">#cfcatch.Message#</span></cfcatch>
		</cftry>
	</li>
	
	<cfset part = xmlGeorge.Name.FirstName.XmlText>
	<li>
		xmlGeorge.Name.FirstName.XmlText: 
		<cftry>
			#XmlGetNodeType(part)#
			<cfcatch type="any"><span style="color:red">#cfcatch.Message#</span></cfcatch>
		</cftry>
	</li>
</ol>
</cfoutput>
</body>
</html>

The page display is shown below:

Code Sample:

XML/Demos/XmlNodes.cfm
<cfset xmlGeorge=XMLParse(ExpandPath("George2.xml"))>
<cfset nameNodes = xmlGeorge.Name.XmlNodes>
<html>
<head>
<title>XmlNodes()</title>
</head>
<body>
<h1>XmlNodes()</h1>
<ol>
<cfoutput>
<cfloop index="i" from="1" to="#ArrayLen(nameNodes)#">
	<li>#XmlGetNodeType(nameNodes[i])# - #IsXmlNode(nameNodes[i])#</li>
</cfloop>
</cfoutput>
</ol>
</body>
</html>

The page display is shown below:

You'll notice that it matters how the element is accessed. When text nodes are accessed through dot notation (e.g, xmlGeorge.Name.FirstName.XmlText), they are not treated as XML nodes. However, when they are accessed through the XmlNodes array, they are. The same is true for comment and CDATA section nodes. To be safe, you should always test a possible node with the IsXmlNode() function before treating it as an XML node.

Next