Using XML ActionScript 3 PDF Print E-mail
User Rating: / 0
PoorBest 
Tuesday, 15 July 2008

Introduction
Often, when handling the xml via the ActionScript 2, that it comes to write something like:
docXML.firstChild.childNodes [2]. childNodes [i]. ChildNodes [j]. ChildNodes [e]. attributes.name
seeking a simple property node xml ... No practice is not it? We will discover in this course, the new way to access this information, ActionScript 3 with a series of simple examples.

II. Reminders on loading external files
In ActionScript 2, we use the XML object to load and parse a document xml. In ActionScript 3, the system has been redesigned, we use the class URLLoader to load the document. * If you do not know load a file ActionScript 3, I invite you now to read chapter II of my tutorial on shipments, by visiting here.
Here, to recall, a simple example of code that will allow us to load the XML file that I will not explain here, a tutorial that already exist there.
/ / We import the necessary classes:
flash.events.IOErrorEvent import;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
flash.events.ProgressEvent import;

/ / We declare an object URLLoader:
var charger: URLLoader = new URLLoader ();

/ / We create an object URLRequest which contains the URL of the file:
var address: URLRequest = new URLRequest ( "/ file / exemple.xml");

/ / We define the file format instructed:
var format = URLLoaderDataFormat.TEXT;
chargeur.dataFormat = format;

/ / We are launching the loading file, indicating the address of that by being URLRequest:
chargeur.load (address);

/ / Defining events of the object charger
chargeur.addEventListener (Event.COMPLETE, finDuChargement);
chargeur.addEventListener (ProgressEvent.PROGRESS, promotion);
chargeur.addEventListener (IOErrorEvent.IO_ERROR, indiquerErreur);

/ / Definition of the function performed by Event COMPLETE:
/ / (Triggered when the loading ends)
finDuChargement function (event: Event) (
Var = event.target.data content;
Trace ( "loading finished!" + Content);
)
/ / Define the function executee by Event PROGRESS:
/ / Triggered by progress of each charge)
function progress (event: Event) (
trace (event.bytesLoaded + "responsible" + event.bytesTotal);
)
/ / Depending on whether an error occurs chargmement:
indiquerErreur function (event: Event) (
Trace (event);
)
We are therefore subject with our loader and its property data that will allow recovery of the contents of the file xml. Please note that the property dataFormat the object URLLoader will be informed of URLLoaderDataFormat.TEXT;

III. The class XML actionScript 3
III-A. Declaring an object of type XML

To declare an object of type XML, ActionScript, several options are available, If you're fan of simplicity, you just have to operate as follows:
var monXML: XML = <root>
<balise Variable="testA" />
<balise Vairbale="testB" />
<tests>
<test1/>
<test2/>
</ Tests>
</ Root>;
Notice the absence of quotation marks! This is more data formatted String.
You can also operate instanciant the manufacturer of Class XML:
var monXML: XML = new XML ();
monXML = <tag> <test/> </ tag>;
Or to instanciant the manufacturer of Class XML, with the content type String (with or without quotation marks quotes in arguments:
var monXML: XML = new XML ( "<tag> <test/> </ tag>");
/ / Or:
var monXML: XML = new XML (<tag> <test/> </ tag>);

III-B. Read an object of type XML
The ActionScript 3 gives us a much more flexible way to access nodes xml that ActionScript 2 and ActionScript 1.
We listerons the main methods / syntax to know while illustrating them with examples.

III-B-1. Go to the principal node
The use of the property firstchild of ActionScript 2 on the object xml becomes unnecessary, it will access the content directly with the proceeding, without worrying about this.
Thus:
var xml: XML = <tag> <test> BlaBla </ test> </ tag>;
trace (xml.test) / / displays "BlaBla"
Please note that to know the name of the principal node, you can use the method localName ();

III-B-2. Go to the object format String
You can use the method toXMLString () to transform your object to object of type String.

III-B-3. Accessing tags
In ActionScript 3, a tag daughter is comparable owned by its parent tag:
var xml: XML = <root> <tag> Test </ tag> </ root>;
trace (xml.balise); / / trace Test
/ / or:
trace (xml.balise [0]); / / trace Test

III-B-4. Go to attribtus a tag
In ActionScript 2, we used. Attributes with access to an attribute tag, in ActionScript 3, to access the attributes of a beacon, we will use the prefix @.
var xml: XML = <root> <balise name="Test" text="Je am a attributs" /> </ root>;
trace (xml.balise. @ name); / / trace Test
Note the syntax equivalent in brackets:

var xml: XML = <root> <balise name="Test" text="Je am a attributs" /> </ root>;
trace (xml.balise. @ [ "name"]); / / trace Test

It is also possible to use the method attribute ();
var xml: XML = <root> <balise name="Test" text="Je am a attributs" /> </ root>;
trace (xml.balise.attribute ( "name")) / / trace Test

III-C. Browse a knot
Again, ActionScript 3 will simplify the task, several ways to go (parser) an XML node are:
var xml: XML = <root>
<balise name="balise1" />
<balise name="balise2"/>
<balise name="balise3"/>
</ Root>;

for (var i: String in xml tag ..) (
Trace (xml.balise [i]. @ Name);
)
You can also use the function for each (var i in Array) (); new, which returns the item and not the index of a node or table:
for each (var ball: XML in xml tag ..) (/ / Let the variable ball, as the name "tag" is already used here
Trace (bal. @ name);
)

More condensed yet, you can write it like this:
trace (xml.. tag. @ name);

This code is very condensed, becomes particularly interesting when you put a condition to find a tag. If we seek the tag with the name attribute = "balise2" we will:
var xml: XML = <root>
<balise name="balise1" />
<balise name="balise2"/>
<balise name="balise3"/>
</ Root>;
var tag: XMLList = xml tag ... (@ name == "balise2");
trace (balise.toXMLString ());

III-D. Working on an object type XML
III-D-1. Add a knot

We no longer have a need to use XMLNodes, we just need to define a monNoeud: XML, then we will add that with the method appendChild ();
var xml: XML = <root> <balise name="Test" text="Je am a attribut" /> </ root>;
var monNoeud: XML = <balise name="noeud" text="Je am a attribut" />;
xml.appendChild (monNoeud);

It is, of course, possible to include data dynamically, for this, use () with your variable.
var xml: XML = <root> <balise name="Test" text="Je am a attribut" /> </ root>;
var name: String = "knots (dynamic this time)";
var monNoeud: XML = <balise name={nom} text="Je am a attribut" />;
xml.appendChild (monNoeud);

IV. Conclusion
This course is coming to an end. You should now be able to handle XML with ActionScript 3.
For technical questions, understanding, help, I ask you to put on the forum, I would be forced not to respond to my mail by lack of time.
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials