Home arrow XML arrow Page 3 - Widget Walkthrough

Widget Walkthrough

The first half of this tutorial introduced you to making a rudimentary but functional widget of the sort you can find on Yahoo's site since its purchase of Konfabulator. In this article, you'll add the finishing touches to increase its functionality.

Author Info:
By: Dan Wellman
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
August 21, 2006
  1. · Widget Walkthrough
  2. · Getting the headlines
  3. · Using loops
  4. · Fine tuning headline retrieval
  5. · Publishing your widget

print this article

Widget Walkthrough - Using loops
(Page 3 of 5 )


The Widget reference manual states that you should use a try catch loop to perform XPath functions, so you can add one to your getData() function.  You will first need to obtain the XML document and place it in a variable using the parse method:

try {
        doc = XMLDOM.parse(feeddata);

Next, you need to specify which elements in the XML file you want to match. This is the XPath element of your function:

        titlelist = doc.evaluate( "rss/channel/item/title" );
        datatextarea.data = "";

You now need to create an array to hold the headlines as separate DOMElement objects:

        titles = new Array();

In this particular rss file, there are always 19 headlines, however, if you were using a different feed, you may not know the number of headlines in advance so it is a good idea to use a for loop that operates on the length of your array:

        for (n = 0; n < titlelist.length; n++) {

The DOMNodeList returned by the XPath function has a built-in property of item() which can be used to specify which DOMElement in the list you are referring to. In this case, we can match the number of the array item with the item we want to store in the array:

          titles[n] = titlelist.item(n);

Finally, each time the loop iterates, you can set the data property of the <textarea> to the data held in the array item. The actual item held in the array is the DOMElement; to get the actual text held in the object you need to address the firstChild of the element.  The JavaScript new line character is also specified (twice) to break up the headlines:

          datatextarea.data += titles[n].firstChild.data + "nn";
      catch(e) {

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials