Name

XmlSearch

Synopsis

                  
                  
                  XmlSearch(XmlDocObj, XPath_exp)

Searches the specified ColdFusion XML document object using an XPath expression. If a successful match is made, the function returns an array of XML document object nodes that match the search criteria. The following example reads in an XML news feed from macromedia.com, parses it into an XML document object, uses XPath to return only the XML nodes containing tutorials, and outputs the results to the browser:

<cfhttp URL="http://www.macromedia.com/devnet/resources/macromedia_resources.xml"
        method="Get">
   
<cfset DesDev = XmlParse(cfhttp.FileContent)>
<cfset SearchResults = XmlSearch(DesDev, "/macromedia_resources/resource[attribute::
type='Tutorial']")>
   
<h2>Available Tutorials</h2>
<cfloop index="i" from="1" to="#ArrayLen(SearchResults)#">
  <cfoutput>
  <a href="#SearchResults[i].URL.XmlText#">#SearchResults[i].Title.XmlText#</a><br>
  </cfoutput>
</cfloop>

For more information on XPath, see http://www.w3.org/TR/xpath or XML in a Nutshell, 2nd Ed. by Elliotte Rusty Harold and W. Scott Means (O’Reilly).

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.