XML 101 in AS3/E4X: Take #1

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;

public class TestXML01 extends Sprite
private var foo:TextField;
private var rssURI:String = “http://rss.cnn.com/rss/cnn_topstories.rss”;
//private var
public function TestXML01()
private function init():void
foo = new TextField();
foo.autoSize = TextFieldAutoSize.LEFT;
foo.width = 600;
//multiline should be true in order to use <br> tags
foo.multiline = true;
foo.wordWrap = true;
foo.htmlText = “loading feed…”;
private function loadXML():void
var urlRequest:URLRequest = new URLRequest(rssURI);
var urlLoader:URLLoader = new URLLoader();

//xml should be, and defaults to type of URLLoaderDataFormat.TEXT;
//trace(“urlLoader.dataFormat: “+urlLoader.dataFormat);
//urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
//Event “complete” is flash.Event.COMPLETE
urlLoader.addEventListener(Event.COMPLETE, onXML);

//There are a whole bunch of other events to handle, among them are (event types):

private function onXML(evt:Event):void
//onXML should try to handle errors at the meantime
//xml object gets returned as a property named “data” of the corresponding urlloader object
//Note we first need to cast it to type of XML first. Then xml becomes the top node of the returned xml document, which in this case is the <rss>…</rss> node, which contains the <channel> node:

var xml:XML = new XML(evt.target.data);
foo.htmlText = “<b>”+e.message+”</b>”;
private function handleXML(xml:XML):void

//XML.ignoreWhitespace = true;
var str:String = “”;
str += “<b>”+xml.channel.title+”</b><br>”;
str +=”<i>”+xml.channel.pubDate+”</i><br><br>”;

//use toXMLString() to inspect a node

//both XML and XMLList are top level objects

//get the list values of children
var itemList:XMLList = xml.channel.item;
//length() is a method of XMLList object
var totalItems:uint = itemList.length();

str +=”<ul>”;

for(var i:uint = 0; i<totalItems; i++)
var title:XML = itemList.title[i];
var link:XML = itemList.link[i];
str +=”<li><a href='”+link+”‘ target=’_news’>”+title+”</a>”;

str +=”</ul>”;
foo.htmlText = str;
//manually destroy str
//str = null;

One Response to “XML 101 in AS3/E4X: Take #1”

  1. name Says:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: