MERGE 2 ONE MEDIA

Thursday, November 20, 2008

Load Multiple XML Files - AS3

I've often built Flash apps that require multiple xml files to load. Here I'm going to show you how to dynamically load multiple RSS feeds into Flash.

Download the source files here.

Here is the xml file that has all the links to my RSS feeds.

<?xml version="1.0" encoding="utf-8"?> <m21m:RSSwidget xmlns:m21m='http://www.m21m.com/schema/m21m'> <m21m:RSSfeeds> <m21m:feed feed="xml/twitter.xml"/> <m21m:feed feed="xml/facebook.xml" /> <m21m:feed feed="xml/blogger.xml" /> </m21m:RSSfeeds> </m21m:RSSwidget>

I will load this file to get all the feed attributes. You can add whatever RSS link you want just add another feed node with a feed attribute to your new RSS feed.

I use "m21m" as a namespace in my xml. You can read about namespace here. Using a namespace is a habit for me now.

Here is my actionscript code.

//each xml file to load var xmlManifest:Array = new Array(); //the xml for each file var xmlDocs:Array = new Array(); //the xml file with all the xml files to be loaded. var RSSWidgetRequest:URLRequest = new URLRequest("xml/rss-widget.xml"); var urlLoader:URLLoader = new URLLoader(); var docsXML:XML = new XML(); docsXML.ignoreWhitespace = true; //when COMPLETE is loaded run function loadDocs urlLoader.addEventListener(Event.COMPLETE,loadDocs); urlLoader.load(RSSWidgetRequest); //load the docs function loadDocs(event:Event):void { docsXML = XML(event.target.data); //m21m is the name space defined in my doc m21m:feed... you don't need one. var m21m:Namespace=docsXML.namespace("m21m"); //get all the feed nodes var feeds=docsXML..m21m::feed; for (var i:int=0; i < feeds.length(); ++i) { //add the feed to the xmlManifest array xmlManifest[xmlManifest.length] = feeds[i].attribute("feed"); } //load the xml for each doc loadXMLDocs(); } //load all the XML files function loadXMLDocs() { if (xmlManifest.length>xmlDocs.length) { var RSSURL:URLRequest = new URLRequest(xmlManifest[xmlDocs.length]); var urlLoader:URLLoader = new URLLoader(); var xmlDoc:XML = new XML(); xmlDoc.ignoreWhitespace = true; urlLoader.addEventListener(Event.COMPLETE,getDoc); urlLoader.load(RSSURL); function getDoc(event:Event) { xmlDoc = XML(event.target.data); //hold all the xml of each doc in an array xmlDocs[xmlDocs.length] = xmlDoc; loadXMLDocs(); } } else { trace(xmlDocs) //do something when all xml is loaded } }

Starting from the top of the code. This is the array that will hold all my RSS files

var xmlManifest:Array = new Array();

I could also write my code like this and delete the loadDocs function but I want this file to work without having to open Flash.

var xmlManifest:Array = new Array("xml/twitter.xml", "xml/blogger.xml", "xml/facebook.xml");

This array will hold all all the xml for each xml file loaded

var xmlDocs:Array = new Array();

Load the rss-widget.xml xml file.

var RSSWidgetRequest:URLRequest = new URLRequest("xml/rss-widget.xml");

Pretty straight forward what this is. If you don't know go to Adobe Live Docs.

var urlLoader:URLLoader = new URLLoader();

This will hold the xml from my rss-widget.xml file.

var docsXML:XML = new XML(); docsXML.ignoreWhitespace = true;

Event Listener waits for the file to complete loading

urlLoader.addEventListener(Event.COMPLETE,loadDocs);

Load the URL.

urlLoader.load(RSSWidgetRequest);

When the xml has loaded loaded run this function

function loadDocs(event:Event):void {

The xml is loaded and a variable referenced

docsXML = XML(event.target.data);

m21m is the namespace defined in my doc <m21m:feed... you don't need one. I use this to define my namespace. Perhaps in the future I will want to add another doc or use my xml with a xsl template to create a different kind of app. Check out namespaces here.

var m21m:Namespace=docsXML.namespace("m21m");

This var holds all the feed nodes

var feeds=docsXML..m21m::feed; <m21m:feed feed="xml/twitter.xml"/> <m21m:feed feed="xml/facebook.xml" /> <m21m:feed feed="xml/blogger.xml" />

There are three feed nodes. Loop through them all.

for (var i:int=0; i < feeds.length(); ++i) {

Add each file to the xmlManifest array

xmlManifest[xmlManifest.length] = feeds[i].attribute("feed"); }

Load the xml for each doc using the loadXMLDocs() function

loadXMLDocs(); }

Run through and load all the xml for each xml file in the xmlManifest arrary

function loadXMLDocs() { if (xmlManifest.length>xmlDocs.length) {

Load the xml file. Same thing is going on here as is going on at the top of the file.

var RSSURL:URLRequest = new URLRequest(xmlManifest[xmlDocs.length]); var urlLoader:URLLoader = new URLLoader(); var xmlDoc:XML = new XML(); xmlDoc.ignoreWhitespace = true;

When complete run the getDoc function

urlLoader.addEventListener(Event.COMPLETE,getDoc); urlLoader.load(RSSURL); function getDoc(event:Event) { xmlDoc = XML(event.target.data);

Hold all the xml for each doc in an array

xmlDocs[xmlDocs.length] = xmlDoc; loadXMLDocs(); } } else {

Now you have all your RSS feeds in one array. You can do whatever you want with the data.

//trace(xmlDocs) } }

I'm going to add another blog with a completed RSS reader soon. If you have comments or questions leave them here in the comments. If you know of a better way to do things comment them also.

Labels: , , ,

1 Comments:

Blogger saddacracker said...

Thanks for explaining it. I'm gonna go read about namespaces while I eat lunch now. Boom.

November 20, 2008 at 12:43 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home