User Guide

Table Of Contents
870 Chapter 35: Using XML and WDDX
The following example extracts all the elements named last, which contain the employees last
names, from the employeesimple.xml file, and displays the names:
<cffile action="read"
file="C:\inetpub\wwwroot\examples\employeesimple.xml"
variable="myxml">
<cfscript>
myxmldoc = XmlParse(myxml);
selectedElements = XmlSearch(myxmldoc, "/employee/name/last");
for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
writeoutput(selectedElements[i].XmlText & "<br>");
</cfscript>
XPath is specified by the World-Wide Web Consortium. For detailed information on XPath, see
the W3C website at www.w3.org/TR/xpath. Most books that cover XSLT also discuss XPath.
Example: using XML in a ColdFusion application
The example in this section shows how you can use XML to represent data, and how ColdFusion
can use XML data in an application. Although the example is too simple to be used in an
application without substantial changes, it presents some of the common uses of XML with
ColdFusion.
The example receives an order in the form of an XML document, processes it, and generates an
XML receipt document. In this case, the order document is in a file, but it could be received as
the result of an HTTP request, or retrieved using
cfpop, cfftp, or other methods. The
ColdFusion page does the following with the order:
1.
Generates a query object from an XML document.
2.
Queries a database table to determine the order discount percentage to use.
3.
Uses a query of queries to calculate the total price, then calculates the discounted price.
4.
Generates the receipt as an XML document.
This example displays the results of the processing steps to show you what has been done.
The XML document
The order.xml document has the following structure:
The root element is named order and has one attribute, id.
There is one customer element with firstname, lastname, and accountnum attributes. The
customer element does not have a body
There is one items element that contains multiple item elements
Each item element has an id attribute and contains a name, quantity, and unitprice element.
The name, quantity, and unitprice elements contain their value as body text.