There are many, many uses for XML, and if you use ColdFusion to consume web services, or create data integration processes, then chances are, you will need work with XML. As of ColdFusion MX there are several tags and functions that make working with XML a snap. In this tutorial I am going to focus on teaching you how to pull the information you need out of XML documents.
There are several ways you might use to read the XML text into a ColdFusion variable. Which one you use depends on the source of the XML. If it's in a database you would use CFQUERY, if it is on a file on the ColdFusion server you would read it with CFILE, if you are downloading it from the web you could use CFHTTP or CFFTP, etc. Ultimately, you will end up with a ColdFusion variable that contains the XML text. For this example, and just to keep it simple, I am going to use CFSAVECONTENT to create that variable. Rather than including this CFSAVECONTENT code in each of my code samples, you can assume that all the XMLFile variables contain this XML.
<title>Macromedia ColdFusion MX 7 Certified Developer Study Guide</title>
<title>Learning XML, Second Edition</title>
<title>Coldfusion MX Developer's Handbook</title>
As you can see, we are going to work with XML that contains a list of books. The first thing we need to do is use XMLParse() to convert the text contained in the "XMLFile" variable into a ColdFusion XML document object. An XML document object is how ColdFusion represents an XML document, and is much like a ColdFusion structure.
If you run the above code you will get the following output:
Now you are at the point where you can start pulling the data that you need out of the XML. The best way to pull the information you need out of a ColdFusion XML document object is to use the XMLSearch() function. XMLSearch uses an XPath language expression to search an XML document object and returns a ColdFusion array containing the matching nodes. XPath is a language for addressing parts of an XML document, and the expressions are pretty easy to write once you spend a little time figuring them out.
The basic XPath syntax is similar to file system addressing. If the path starts with a "/", then it represents an absolute path to the element you want.
So if we want to get all the "book" nodes from our XML we could use:
<cfset BookNodes = xmlSearch(MyXMLDoc,'/books/book')>
<cfloop from="1" to="#arraylen(BookNodes)#" index="i">
<!--- The array contents need to parsed so you can easily get at
the child nodes children and attributes. --->
<cfset BookXML = xmlparse(BookNodes[i])>
If the path starts with "//" then all elements in the document which fulfill criteria following the "//" are selected. So, if we just wanted to pull a list of authors from our XML, we could use:
<cfset Authors = xmlSearch(MyXMLDoc,'//author')>
<cfloop from="1" to="#arraylen(Authors)#" index="i">
<cfset AuthorXML = xmlparse(Authors[i])>
With more advanced XPath expressions you can do things like search for nodes with specific values. Here is an example of how to find all the books with titles that contain "ColdFusion". (Keep in mind that these contains() searches are case sensitive.)
<cfset SearchByTitle = xmlSearch(MyXMLDoc,'//book [contains(title,"ColdFusion")]')>
<cfloop from="1" to="#arraylen(SearchByTitle)#" index="i">
<cfset BookXML = xmlparse(SearchByTitle[i])>
There are many other ways you can use XMLSearch and XPath to pull the data you need from your XML. To learn more about them visit http://www.w3.org/TR/xpath, or just search Google for "XPath Tutorial" and you will find several good ones.