How to Load Data and Use it From XML

Create the FLA
Victor Gaudioso
Victor Gaudioso is a Sr. Application developer for an advertising firm in Hollywood, CA. He specializes in Flash / ActionScirpt but also programs in other languages including but not limited to C#, XAML, WPF and ASP .NET. He has engineered Flash sites for the major entertainment studios including Disney, Universal, TouchStone, Mattel and Warner Bros. among others. Victor is known as dvlnblk in the http://actionscript.org forums and has recently been appointed a site moderator. AIM: dvlnblk2004 Yahoo: victoratdeadline
View all articles by Victor Gaudioso
Then create an AS layer and paste in this code:
// create the XML variable
var xml:XML = new XML();
// you must ignore whitespace
xml.ignoreWhite = true;
// the function that is called when the xml is loaded
xml.onLoad = function() {
// tells you the number of child nodes
var nodes = this.firstChild.childNodes;
// tells you how many items you have
numOfItems = nodes.length;
// attach icons
for (var i = 0; i<numOfItems; i++) {
// attach image to the myImageHolder MovieClip
myImageHolder.loadMovie(nodes[i].attributes.image);
// set the text
myText.text = nodes[i].attributes.caption;
}
};
// load the xml
xml.load("xml/images.xml");
If you run this you will see that the content is now populated from the xml page.

Simple as that.
Spread The Word
Attachments
12 Responses to "How to Load Data and Use it From XML" 
|
said this on 14 Mar 2007 12:58:18 PM CDT
I've always found this to be a neater way to loop through loaded XML:
my_xml.onLoad = function() { for(var i = this.firstChild.firstChild; i; i = i.nextSibling) { trace(i.nodeName + ": " + i.firstChild.nodeValue); } }; |
|
said this on 09 Apr 2007 4:37:44 AM CDT
it's better to check the load result with onLoad fuction:
obj.onLoad = function(success:Boolean):Void { if (success) { if (this.status == 0) { // success } else { // failed to parse the xml } } else { // path not found } } |
|
said this on 11 May 2007 6:21:43 AM CDT
Works only for jpg. Failed with gif
|
|
said this on 13 Jun 2007 12:03:23 PM CDT
I'm an extremely new user of Flash and Actionscript, and I have a very basic question:
What is an "AS Layer" and how do I create on? -Thanks |
|
said this on 17 Jun 2007 4:23:32 PM CDT
I was disappointed that this didn't work for GIFs. :(
"What is an "AS Layer" and how do I create on?" I assume "AS Layer" is short for "ActionScript Layer." Such a layer would have ActionScript code applied to a frame (or frames) on that layer, but the layer would not be used for anything else (such as text or image placement/animation). |
|
said this on 09 Jul 2007 8:17:03 PM CDT
How can I change the image size. Do I have to process the image to the desired width and height or can I change it
|
|
said this on 12 Jul 2007 4:52:00 AM CDT
I keep getting an error report »
1120: Access of undefined property numOfItems on numOfItems = nodes.length; and a second for the source of » for (var i = 0; i<numOfItems; i++) |
|
said this on 12 Jul 2007 8:32:04 AM CDT
Weirdly the downloaded zip doesn't populate the xml either.
|
|
said this on 07 Aug 2007 6:09:15 PM CDT
Had a similar error and changed it to a AS 2.0 file and it worked fine.
|
|
said this on 03 Oct 2007 10:55:05 AM CDT
I would like some further explanation as to what parts of the code relate to what parts of the xml, how to load in more complex xml and something along those lines.
|
|
said this on 03 Dec 2007 10:17:50 PM CDT
Thank you. This was very helpful. I am wondering though, how do I add a loop to this code? I want the last image to be followed by the first image in the XML file.
Thanks. |
|
said this on 15 Sep 2008 3:16:09 PM CDT
Couldn't get it to work at all!!!
|



Author/Admin)