| By Richard Gorremans | Article Rating: |
|
| May 19, 2004 12:00 AM EDT | Reads: |
11,836 |
In Part 1 of this two-part article (MXDJ, Vol. 2, issue 3) I showed how to invoke a Web service for the purpose of validating user input. Part 2 will delve a little deeper into the ColdFusion MX language structure specifically designed to handle XML result sets such as those returned from a Web service. The goal is to consume a Web service that will return headline news articles for a user-specified topic. When finished you will be able to add a live user-interactive newsfeed to your Web site.
This article will cover:
- XmlParse(): Returns an XML Document object
- XmlRoot property: Root element for an XML Document object
- XmlName property: Element name
- XmlChildren[1]: Referencing the first row in an array containing the child nodes for a specified element
- XMLSearch(): Search routine that returns an array of nodes that match a provided XPath expression
- Error trapping with <cftry> and <catch>
One resource for locating Web services is located at www.xmethods.com, where you will find the Web service (www.xmlme.com/WSCustNews.asmx?WSDL) that will be used for this article. This particular Web service returns a string that is in an XML format; other Web services may return different values. The Axis engine that is built into ColdFusion MX will deserialize these return values, creating a data type you can use.
Testing the Web Service
Now that the decision has been made as to what we want done and where to find the Web service that will provide the necessary information, the next step is testing the Web service. Testing will answer two questions.
- Does the Web service work?
- What is the Document Tree for the returned result set?
A new entry will be added to the Component tab of the Application panel. Click on the new entry to view the document tree for the WSDL file (see Image III).
Using the file administrator, open a new document and drag-and-drop the new Web service into the new document as shown in Image IV.
You have now created the basic coding necessary for consuming the Web service. The code sample created by Dreamweaver MX shows one argument being passed to the Web service (topic), and the result set returned from the Web service will be stored in the aString variable. Replace "enter_value_here" with the word "Sacramento", which will provide a topic for the Web service to search on for the test.
The next step will be to add the coding to display the results returned from the Web service (see Image V). For displaying the results the XmlParse() function and <CFDUMP> tag are used. XmlParse() is a new function that accepts XML code as a string and returns an XML Document object that represents the various XML elements contained in the document. Image VI shows the results of the <CFDUMP>.
The User Interface
Now that the Web service has been selected and tested, the next step is to create an interface where the user can enter a topic of his or her choice and display the resulting headlines. The following list is a very simplified view of the desired process flow for the user interface.
- Display entry field for user to enter topic.
- Invoke Web service, passing user input as an argument and using a default value for topic if one is not provided.
- Display the result set if there are no errors.
- Display a message if there is an error.
Another parameter will be the number of articles returned by the Web service, defaulting to zero. If the Web service returns a result set, this variable will be populated with a count of the number of headlines received.
<CFPARAM NAME="sTopic" DEFAULT="" />
<CFPARAM NAME="xCount" DEFAULT=0 />
With the default values out of the way the next step is to create the GUI. This will consist of a title, one label, an input field for the topic, and a submit button. This will be a rollover form so the CGI.SCRIPT_NAME variable is used to force the page to call itself on post (see Code I). Image VII shows the user input form for topic.
Consuming a Web service, just like accessing a database, should be treated as if there is a good chance the service will be unavailable, the return results are corrupted, or there are no results returned. With a database error you typically receive an error code that can be responded to. When you attempt to consume a Web service, the errors will vary and may be hard to account for. These potential problems are easily dealt with by placing the <CFINVOKE> code inside a <CFTRY>.
The entire code segment is placed inside a conditional statement that prevents the code from being executed unless the form has been submitted and a topic has been provided by the user (see Code II).
In Code II new properties (XmlRoot, XmlName, XmlChildren) and the XMLSearch() function have been introduced. The coding combination ensures that if the provider of the Web service changes the root or child element the program still has a good chance of retrieving the desired information without any modifications.
<cfset xRoot = MyXml.Xmlroot>
Retrieves information about the root element.
<cfset bNode = xRoot.XmlName>
Retrieves the name of the root element. In this case it is "moreovernews" (see Image VI).
<cfset cNode = xRoot.XmlChildren[1].XmlName>
Retrieves the name of the first child element. In this case it is "article" (see Image VI).
The combined results of these three <CFSET> calls provide the second parameter needed for the XMLSearch() function. This function will return an array containing the headlines received from the Web service.
The next line (<cfset xCount = arraylen(myNewsFeed)> ) returns a count of the number of elements (headlines) the XMLSearch() function found. This count will be used for a looping routine to display the result set and controlling whether or not the results code segment is executed.
The next step is to display the headlines to the user. The displaying of this code will be determined by the value of the xCount variable (see Code III). If the Web service returns a result set the value will be greater than zero and can be displayed (see Image VIII). A looping routine will be used to iterate over the result set, displaying the headline_text, source, and harvest_time elements. The url element will be used to create a hyperlink to view the entire story in a separate window (see Image VI).
Typically there are two types of problems with which a Web service will most likely return an error.
- A connection to the Web service cannot be made.
- The Web service does not know how to deal with the information provided as the argument (such as a topic where no headlines are found).
Summary
Once you start working with the code presented in this article you will find that there are endless possibilities for creating new types of dynamic user-interactive Web pages. Companies such as Amazon.com and eBay have already started providing access to their engines via Web services that will greatly shorten development time. For example, with minimal programming, you can create fully operational e-commerce sites using another company's stock and their Web services.
In my next article you'll learn how to shorten your development time even more by taking this same Web service and applying an XSLT style sheet. Once it's applied, you'll find that your completed Web pages can be more dynamic and, with a few simple conditional statements, display the same information in an unlimited number of formats.
Published May 19, 2004 Reads 11,836
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Richard Gorremans
For the past four yers, Richard Gorremans has been working for EDFUND, the nonprofit side of the Student aid Commission, located in Rancho Cordova, California. A senior software engineer with over 13 years in the business, he has been working as a technical lead producing Web-based products tht enable borrowers, lenders, and schools to view and maintain student loan information via the Web.
- Contrary Opinion: Why Silverlight is Good for Adobe
- Ulitzer Live! New Media Conference & Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- My Thoughts on Ulitzer
- Analytics for Adobe Air Applications
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Software Flexibility in the Cloud - Part 4 of 5
- Cloud Executives Feature on Cloud Computing Expo Power Panel
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Java Kicks Ruby on Rails in the Butt
- Interviewing Java Developers With Tears in My Eyes
- Adobe Enters Cloud Computing with LiveCycle
- Social Media Terrorists
- Adobe Flash Media Server on iPhone
- Ruby-on-Rails Apps Get Cloud Lift
- Contrary Opinion: Why Silverlight is Good for Adobe
- Adobe Flex 4 Goes to Public Beta
- Flexing Your .NET 3.5 Skillset
- Where Are RIA Technologies Headed in 2008?
- Cover Story: How to Increase the Frame Rates of Your Flash Movies
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Your First Adobe Flex Application with a ColdFusion Backend
- Adobe Flex 2: Advanced DataGrid
- i-Technology Blog: Death-Knell For "Rich Media? Hardly!
- Adobe/Macromedia - Microsoft, Look Out!
- How To Create a Photo Slide Show ...
- Adobe Flex Interface Customization - Themes, Styles, Skins
- Personal Branding Checklist

































