Adobe COLDFUSION 9 Manual page 1089

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Using Web Elements and External Objects
Code
<cffile action="read"
file="C:\CFusion\wwwroot\examples\order.xml"
variable="myxml">
<cfset mydoc = XmlParse(myxml)>
<!--- Extract account number --->
<cfset
accountNum=#mydoc.order.customer.XmlAttributes.acc
ountNum#>
<cfset orderquery = QueryNew("item_Id, name, qty,
unitPrice") >
<cfset temp = QueryAddRow(orderquery, #numItems#)>
<cfloop index="i" from = "1" to = #numItems#>
<cfset temp = QuerySetCell(orderquery, "item_Id",
#mydoc.order.items.item[i].XmlAttributes.id#,#i#)>
<cfset temp = QuerySetCell(orderquery, "name",
#mydoc.order.items.item[i].name.XmlText#, #i#)>
<cfset temp = QuerySetCell(orderquery, "qty",
#mydoc.order.items.item[i].quantity.XmlText#,
#i#)>
<cfset temp = QuerySetCell(orderquery,
"unitPrice",
#mydoc.order.items.item[i].unitprice.XmlText#,
#i#)>
</cfloop>
<cfquery name="discountQuery"
datasource="cfdocexamples">
SELECT *
FROM employee
WHERE Emp_Id = #accountNum#
</cfquery>
<cfset drate = 0>
<cfif #discountQuery.RecordCount# is 1>
<cfset drate = 10>
</cfif>
<cfquery name="priceQuery" dbType="query">
SELECT SUM(qty*unitPrice)
AS totalPrice
FROM orderquery
</cfquery>
<cfset discountPrice = priceQuery.totalPrice * (1
- drate/100)>
<cfxml variable="receiptxml">
<receipt num = "34">
<cfoutput>
<price>#discountPrice#</price>
<cfif drate GT 0 >
<discountRate>#drate#</discountRate>
</cfif>
</cfoutput>
<itemsFilled>
<cfoutput query="orderQuery">
<name>#name# </name>
<qty> #qty# </qty>
<price> #qty*unitPrice# </price>
</cfoutput>
</itemsFilled>
</receipt>
</cfxml>
Moving complex data across the web with WDDX
WDDX is an XML vocabulary for describing a complex data structure, such as an array, associative array (such as a
ColdFusion structure), or a recordset, in a generic fashion. It lets you use HTTP to move the data between different
application server platforms and between application servers and browsers. Target platforms for WDDX include
ColdFusion, Active Server Pages (ASP), JavaScript, Perl, Java, Python, COM, Flash, and PHP.
Description
Reads the XML from a file and convert it to an XML document object.
Sets the accountNum variable from the customer entry's accountnum
attribute.
Converts the XML document object into a query object.
Creates a query with columns for the item_id, name, qty, and unitPrice
values for each item.
For each XML item entry in the mydoc.order.items entry, fills one row
of the query with the item's id attribute and the text in the name,
quantity, and unitprice entries that it contains.
If the account number is the same as an employee ID in the
cfdocexamples database Employee table, the query returns one
record. and RecordCount equals 1. In this case, sets a discount rate of
10%. Otherwise, sets a discount rate of 0%.
Uses a query of queries with the SUM operator to calculate the total
cost before discount of the ordered items, then applies the discount to
the price. The result of the query is a single value, the total price.
Creates an XML document object as a receipt. The receipt has a root
element named receipt, which has the receipt number as an attribute.
The receipt element contains a price element with the order cost and
an itemsFilled element with one item element for each item.
Last updated 8/5/2010
1084

Advertisement

Table of Contents
loading

Table of Contents