Adobe
产品
Acrobat
Creative Cloud
创意套装
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
更多产品
解决方案
数字营销
数字媒体
教育
金融服务业
政府部门
网页体验管理
更多解决方案
学习帮助下载公司
商店
在线商店
批量许可
查找经销商
搜索
 
信息 登录
欢迎,我的支持
我的帐户
注销
为何登录?登录后可以管理您的帐户,访问试用版下载、产品扩展和社区区域等。
Adobe
产品 分类 购买   搜索  
解決方案 公司
学习
登录 注销 我的货物 我的支持
Date Date
Qty:
Subtotal
Checkout
Adobe 开发者中心 / Dreamweaver 开发人员中心 /

Creating a Spry XML data set

by Raymond Camden

Raymond Camden
  • coldfusionjedi.com

Content

  • A basic example
  • A dynamic example

Created

2 March 2009

页面工具

在 Facebook 上共享
在 Twitter 上共享
在 LinkedIn 上共享
书签
打印

Tags

要求

必备知识

Basic HTML and XML knowledge and familiarity with the Dreamweaver workspace, site management, and building websites.

用户级别

中级

必需产品

  • Dreamweaver CS4 (Download trial)
  • ColdFusion 8 (Download trial)

范例文件

  • xml_dataset_sample.zip (3 KB)

In Creating an HTML data set, I demonstrated how easy HTML data sets are to use in Dreamweaver CS4. HTML data sets are tables, or other structured bits of HTML, that Spry can load via Ajax. Once loaded, this data can be sliced and diced (that is, sorted, filtered, and more) in a variety of ways, making it rather easy to develop dynamic Ajax applications without needing any knowledge of JavaScript or server-side technology.

In this article I'll introduce XML data sets. I'll demonstrate that using XML data sets is as easy as using HTML data sets in Dreamweaver CS4. I'll also discuss how you can use ColdFusion to dynamically create the XML used by Spry.

Note: Before you begin, be sure you have a website defined in Dreamweaver and then download the sample files that accompany this article. Unzip the files into a new folder. You can name this anything you want; I used the name dwarticlexml. You will also need to have ColdFusion 8 installed and running with the site used in Dreamweaver. Be sure you install the example applications with ColdFusion as the database used for this tutorial's dynamic example will use them. For more information about defining a site, refer to Setting up a Dreamweaver site in Dreamweaver Help. For help with ColdFusion, read Setting up a ColdFusion development environment.

A basic example

Let's start by looking at some sample XML data that you will be using for your Spry data sets. In Dreamweaver, open the file jedilist.xml, which is part of the sample files that accompany this article. Here is a snippet of the XML code with a few rows removed to save space:

<?xml version="1.0" encoding="UTF-8"?> <jedis> <jedi> <name>Raymond Camden</name> <age>35</age> <alignment>dark</alignment> </jedi> <jedi> <name>Scott Stroz</name> <age>40</age> <alignment>light</alignment> </jedi> <jedi> <name>Todd Sharp</name> <age>32</age> <alignment>light</alignment> </jedi> <jedi> <name>Scott Pinkston</name> <age>36</age> <alignment>dark</alignment> </jedi> </jedis>

The XML file contains a list of Jedis. Each Jedi has a name, age, and alignment (dark or light; the dark ones are the fun ones). XML is structured data. Note that each person begins with a <jedi> tag and end with </jedi>. Each name is wrapped with a <name> tag, and so on. It is critical that the XML you use be well formed. Although HTML can be a bit forgiving, XML is very strict about how you structure the file. Next, create a new file in Dreamweaver.

  1. Right-click inside the folder you created and select New File. Name your new file test.html. Double-click the file to open it in Dreamweaver.
  2. Place your cursor after the <body> tag. Ok, now for the fun part.
  3. On the Insert menu, select Data > Spry Data Set. This opens the window shown in Figure 1.
The default window for inserting a Spry Data Set.
Figure 1. The default window for inserting a Spry Data Set.
  1. By default Dreamweaver will want to create an HTML data set. Simply switch the Data Type to XML.
  2. Click the Browse button to tell Dreamweaver where to find your data file. Select jedilist.xml from your site folder. Dreamweaver will read the file and display a tree-style view of your XML data (see Figure 2).
Dreamweaver has parsed in the XML and understands the basic structure of your data.
Figure 2. Dreamweaver has parsed in the XML and understands the basic structure of your data.
  1. Ok, now note the data preview. Although it looks kind of right, it only seems to see one row of data. That's because the Row element selected above is "jedis", not "jedi". Remember, you wrapped the entire XML file in the jedis tag, but each individual jedi was wrapped in a jedi tag. Figure 3 shows what happens when you select the jedi node.
Now Dreamweaver knows how to list out your data from the XML file.
Figure 3. Now Dreamweaver knows how to list out your data from the XML file.

Dreamweaver has recognized that the jedi tag wraps each individual row of data. The preview confirms this. Click Next.

  1. If you stepped through Creating an HTML data set, then this part of the wizard should be familiar. Here you can tell Spry how to treat each column.

    This is a purely optional step, but it helps with sorting if you tell Spry when columns aren't basic string types. Your data is rather simple. Only the age column is numeric. So go ahead and select the age column and select Number from the Type drop-down menu. Next, set the Sort column (towards the bottom) to Name. This will sort the XML data by the name column.

Specifying settings for the columns and sorting.
Figure 4. Specifying settings for the columns and sorting.
  1. Click next to the go to the last step. Under insert options, select Insert Table and click the Done button.

Your HTML page should look like this:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:spry="http://ns.adobe.com/spry"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script src="../SpryAssets/xpath.js" type="text/javascript"></script> <script src="../SpryAssets/SpryData.js" type="text/javascript"></script> <script type="text/javascript"> <!-- var ds1 = new Spry.Data.XMLDataSet("jedilist.xml", "jedis/jedi", {sortOnLoad: "name", sortOrderOnLoad: "ascending"}); ds1.setColumnType("age", "number"); //--> </script> </head> <body> <div spry:region="ds1"> <table> <tr> <th spry:sort="name">Name</th> <th spry:sort="age">Age</th> <th spry:sort="alignment">Alignment</th> </tr> <tr spry:repeat="ds1"> <td>{name}</td> <td>{age}</td> <td>{alignment}</td> </tr> </table> </div> </body> </html>

If you open your browser to this file, you can now see the XML data has been dynamically loaded into the HTML. The data was sorted automatically by the name, but you can actually click the header of each column and sort the data any way you like.

A dynamic example

In the previous example, I demonstrated Spry working with an XML file that is hard coded on the file system. For people without a proper application server like ColdFusion, this manual solution may be the only solution they have. But if you do have ColdFusion installed, then why not use the power of CFML to help you create dynamic XML to be used within your Spry applications? Let's look at how you can use ColdFusion to generate XML.

Note: For help with setting up a ColdFusion application server, read Setting up a ColdFusion development environment.

  1. Create a new file named artdata.cfm. Open it and remove the default HTML that Dreamweaver put into the file.
  2. Select the Server Behaviours tab in Dreamweaver. You'll see a list of items that you may, or may not, have to complete to tell Dreamweaver about your local ColdFusion installation. This should be a pretty simple set of tasks and will only need to be done once. If your Server Behaviours panel looks like the one in Figure 5, you are all set.
Figure 5. A properly configured Server Behaviors panel leaves you feeling well rested and assured that everything will turn out OK in the end.
Figure 5. A properly configured Server Behaviors panel leaves you feeling well rested and assured that everything will turn out OK in the end.
  1. As item 6 says in the Server Behaviors panel, click the + button and choose Recordset. This opens a panel where you can configure a database query. For the name, use Art. For the database, select cfartgallery. The table should already be set as APP.ART (see Figure 6). Leave columns as All and click OK.
Settings for the new Recordset.
Figure 6. Settings for the new Recordset.

Dreamweaver will insert the following query into your template.

<cfquery name="Art" datasource="cfartgallery"> SELECT * FROM APP.ART </cfquery>
  1. Now you need to generate XML from this data. You don't have enough time to do a full explanation of the code below. Also note that there are multiple ways to generate XML within ColdFusion. Your code will simply create an XML document from the query and serve it to the user. Copy and paste the following code to the file, after the query.
<cfxml variable="xml"> <art> <cfoutput query="art"> <piece> <name>#artname#</name> <description>#description#</description> <price>#price#</price> <image>#largeimage#</image> </piece> </cfoutput> </art> </cfxml> <cfcontent type="text/xml" reset="true"><cfoutput>#xml#</cfoutput>

The <cfxml> tag is used to create an XML document from text inside it. I wrap the outer portion of my XML with the <art> tag. Next I loop over the query using <cfoutput>. I wrap each record with <piece> tags and then output a few of the columns. (Note that the <cfsavecontent> tag could have been used instead of <cfxml>. ColdFusion provides many ways to solve common problems!)

  1. Save the file and open it in your browser. You should see generated XML from the art information in the database. I selected to output the name, price, and largeimage columns from the table. I could have chosen other columns as well. What you select from the database and how you output it in XML is really up to your application.

Ok, so now you have ColdFusion generating XML from the database. Even if you aren't a ColdFusion developer (not yet anyway), I hope you can appreciate how simple it was to create the XML dynamically.

Note: To find out how to generate XML easily from ColdFusion queries (and other forms of data), refer to my project, toXML. This is a simple ColdFusion Component (CFC) that handles the grunt work of creating XML.

Next, you want to get it into an Ajax-based application.

  1. Create a new file in Dreamweaver and name in test2.html.
  2. Click right after the body tag.
  3. On the Insert menu, select Spry Data Set. Switch the type to XML and select artdata.cfm. As with the previous example, Dreamweaver can parse the XML, but you have to actually click the piece item to let Dreamweaver know what part of the XML represents a row. Your screen should resemble Figure 7.
First step in inserting our new dynamic XML.
Figure 7. First step in inserting our new dynamic XML.
  1. Click Next. Once again you have the option to tell Dreamweaver how to treat the data. Select the price column and set the type to number. Under Sort Column, set the value to Name. Click Next.
  2. In the previous example you told Dreamweaver to insert a table. This time you are going to get a bit fancy and create a master/detail layout—select this option (it's the second one), and then click Setup.

    What this option screen represents is how the page will determine what is shown in the master list versus what is shown in the detail (see Figure 8). Dreamweaver used the first column, name, for the master list. This means your page will create a list of names. You can then click that name to get a detail of the other columns: description, price, and image. You can choose to modify this, but the defaults are actually rather good, so for now, just click OK. Click Done and Dreamweaver inserts the code onto the page.

Setup options for a Master/Detail layout.
Figure 8. Setup options for a Master/Detail layout.
  1. Before you view the page, you need to make one quick change. You included the image of the art piece on the page, but it would be nice to show the actual picture instead of just the file name. Find the following snippet in the file:
<div class="DetailColumn">{image}</div>

...and change it to:

<div class="DetailColumn"><img src="/cfdocs/images/artgallery/{image}"></div>

This creates a dynamic image based on the data that Spry loads in from the XML.

  1. To finish, save the file and view it in your browser.

Note that you can now click and browse the art pieces—all without writing one line of JavaScript! (Well, obviously some JavaScript was involved, but Dreamweaver wrote it for you!)

Note: In addition to HTML and XML, Spry also supports TSV and CSV data sets. These are tab- and comma-separated files. Although not common, if you have data in that format you can make use of it in Spry. Spry also supports JSON. JSON is probably the best way to create data for Ajax-based applications as it tends to have the smallest size footprint. Dreamweaver doesn't support easy creation of JSON-based data sets; however, once you get more comfortable with it you should definitely look into it. ColdFusion 8 added native support for generating JSON.

Where to go from here

In this article, I demonstrated how easy Dreamweaver makes it to work with Spry. For people uncomfortable with JavaScript, Dreamweaver CS4 goes a long way to making it as easy as a mouse-click to develop Ajax applications. To learn more, check out the Spry Technology Center and the Spry site on Adobe Labs.

More Like This

  • Creating a multiscreen theme for WordPress using Dreamweaver CS5.5
  • Understanding Spry basics
  • Using the Adobe Widget Browser
  • Dreamweaver templates: Customizable starter designs for web designers
  • Setting up a ColdFusion development environment for Dreamweaver
  • Creating master and detail ColdFusion pages
  • Creating user-defined functions for ColdFusion 8 in Dreamweaver CS4
  • Creating a ColdFusion upload page in Dreamweaver CS4
  • Creating custom server behaviors and Dreamweaver extensions
  • Creating your first website – Part 5: Adding the Spry menu

产品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • 移动应用程序
  • Photoshop
  • Touch Apps

解决方案

  • 数字营销
  • 数字媒体
  • 网页体验管理

行业

  • 教育
  • 金融服务业
  • 政府部门

帮助

  • 产品帮助中心
  • 订货和退货
  • 下载和安装
  • 我的 Adobe

学习

  • Adobe 开发人员连接
  • Adobe TV
  • 培训和认证
  • 论坛
  • 设计中心

购买方式

  • 在线商店
  • 批量许可
  • 查找经销商

下载

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

公司

  • 新闻编辑室
  • 合作伙伴计划
  • 公司社会责任
  • 工作机会
  • 投资者关系
  • 事件
  • 法律
  • 安全
  • 联系 Adobe
选择您的地区 中国(更改)
选择您的地区 关闭

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

使用条款 | 隐私政策和 Cookies (更新)

京 ICP 备 10217899 号 京公网安备 110105010404