Home > Actionscript 3 > Tutorial actionscript 3 – Load and read XML file, loading and reading

Tutorial actionscript 3 – Load and read XML file, loading and reading

March 8th, 2009

Reading a forum actionscript http://www.forosdelweb.com/f16/. I found repeated a post asking how to load XML. That motivates me to write the next post.

For those who know nothing about XML stands for Extensible Markup Language, to explain in a simple manner that most are not text files that follow certain standardizations to store data. The syntax rules we impose on xml, allows us to read these files from any language. I am not a scholar on the basic theory of the xml, just give them a good use in my programs. For those who want to study a bit more on xml can be read here. XML IN WIKIPEDIA

XML EXAMPLE:
xmlcode
As you can see the format is quite clever, using a format tag enclosing symbols between greater and less in the identifiers and values, who know html sure you get to fly.

Here a example of the usage. I will continue working on this example to see how to create a gallery of images with this xml.

VER EJEMPLO / VIEW EXAMPLE
DESCARGAR EJEMPLO / DOWNLOAD EXAMPLE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
 
	/**
	 * This class is a little example of hot to work with xml files
	 * @author http://as3.miguelmoraleda.com
	 */
	public class Main extends Sprite
	{
 
		private var _xml:XML;
 
		public function Main()
		{
			super();
 
			loadXMLFile();
		}
 
		private function loadXMLFile():void
		{
			var loader= new URLLoader(new URLRequest("datos.xml"));
			loader.addEventListener(Event.COMPLETE, loadedCompleteHandler);
		}
 
		private function loadedCompleteHandler(e:Event):void
		{
			e.target.removeEventListener(Event.COMPLETE, loadedCompleteHandler);
			_xml = XML(e.target.data);
			for each (var picture:XML in _xml.pic) {
				statusField.text += "========================================\n";
				statusField.text += "Imagen: " + picture.image + "\n";
				statusField.text += "Nombre: " + picture.nombre + "\n";
				statusField.text += "Anio: " + picture.ano + "\n";
 
				trace("Imagen: " + picture.image);
				trace("Nombre: " + picture.nombre);
				trace("Anio: " + picture.ano);
			}
		}
	}
}

miguelMoraleda Actionscript 3

  1. Nav
    August 7th, 2009 at 06:36 | #1

    Where is statusField?

Get Adobe Flash playerPlugin by wpburn.com wordpress themes