Exploring XML and RSS in Flash (4/4) - exploring XML | WebReference

Exploring XML and RSS in Flash (4/4) - exploring XML

Exploring XML and RSS in Flash

Creating the Flash RSS Parser Movie (FRSSPM)

Let's put the newly acquired knowledge on XML in Flash to good use by creating an RSS movie. Here are the instructions:

  1. Download the Actionscript rss_parser.as.
       0 //$Id: rss_parser.as,v 1.2 2003/05/19 11:17:06 filti Exp $
       1 
       2 // declaring variables
       3 var channelTag = "channel";
       4 var titleTag = "title";
       5 var itemTag = "item";
       6 var document = new XML();
       7 
       8 function getElementsByTagName(xmlDoc, tagname, xmlObjArray){
       9     var Nodes = null;
      10     if(xmlObjArray == null){
      11         Nodes = new Array();
      12     }else{
      13         Nodes = xmlObjArray;
      14     }
      15 
      16     if(xmlDoc.hasChildNodes()){
      17         for(var i=0; i < xmlDoc.childNodes.length; i++){
      18             if( (xmlDoc.childNodes[i].nodeType == 1) &&
      19                 (xmlDoc.childNodes[i].nodeName == tagname)){
      20                 Nodes.push(xmlDoc.childNodes[i]);
      21             }
      22             getElementsByTagName(xmlDoc.childNodes[i], tagname, Nodes);
      23         }
      24     }
      25     return Nodes;
      26 }
      27 
      28 
      29 function myLoadHandler(success){
      30 
      31     if(success && document.status == 0){
      32         // retrieving the channel element, get the content of the title
      33         // tag and setting the textfield named "channeltitle" in our Movie
      34         var channel = getElementsByTagName(document, channelTag, null)[0]
      35         _level0.channeltitle = getTitleText(getFirstTitleElement(channel));
      36 
      37         // retrieving all items from the rss file
      38         var itemsArr = getElementsByTagname(document, itemTag, null);
      39 
      40         // this the y-position for the first entry generated from items
      41         var yPos = 20;
      42 
      43         //iterating over the array
      44         for(var i=0; i < itemsArr.length; i++){
      45             //place an instance of the symbol on the stage
      46             //Note: the third argument (depth) means the z level
      47             // in which the new movie is placed
      48             _root.attachMovie("itemtemplate", "clip" + i, i+10);
      49 
      50             with(eval("clip" + i)) {
      51                 //setting the textfield of the new movie
      52                 textfield = getTitleText(getFirstTitleElement(itemsArr[i]));
      53                 // place the movie
      54                 _y = yPos;
      55                 //set the position for the next instance of the symbol
      56                 yPos += _height;
      57             }
      58         }
      59      }else{
      60         _level0.channeltitle = document.loaded + " " + document.status;
      61      }
      62 }
      63 
      64 //returns the node value of the first child of a node
      65 //assumption is: first child is a textnode and has a value
      66 function getTitleText(titleElement){
      67     return titleElement.firstChild.nodeValue;
      68 }
      69 
      70 //returns the first node with tag title of a xml object
      71 function getFirstTitleElement(xmlObjArray){
      72     return getElementsByTagName(xmlObjArray, titleTag, null)[0];
      73 }
      74 
      75 document.onload = myLoadHandler;
      76 document.load("xml/index.rss");
  2. Include the script in the movie with the Frame Actions panel.
  3. Create textfield with dummy text.
  4. Scale the textfield and place it in the movie clip using the Info panel.
  5. In the Text Options panel select "Dynamic Text", "Single Line" and set the Variable to "channeltitle".
  6. Create a Symbol of the Movie Clip containing one textfield.
  7. Scale the textfield and place it in the movie clip using the Info panel.
  8. In the Text Options panel select "Dynamic Text", "Single Line" and set the Variable to "textfield".
  9. In the Symbol panel set the name to whatever you want.
  10. To access the symbol by name from the Actionscript, you need to set the name in the Symbol Linkage Property dialog box. Set the name to "itemtemplate".
The FRSSPM is available here for your Webvergnügen.
It can also be downloaded.
Note: Scaling the textfields in the Info panel has some sideeffects when playing the FRSSPM in Netscape or Mozilla on Linux with plugin version 5r48. See the screenshot:

Conclusion

The Flash XML parser allows Flash movies to receive and manipulate XML data on-the-fly. It can handle small, wellformed XML documents and presents a lightweight, DOM-oriented but proprietary API.

Resources

About the Author

Andreas "Filti" Woitzick is a freelance software developer focusing on tools and architecture for Web applications. He enjoys wrestling with Flash to create friendly applications, rather than just scary movies...


Produced by Michael Claßen

URL: https://www.webreference.com/xml/column82/4.html
Created: May 26, 2003
Revised: May 26, 2003