Not yet rated

Problem

Before we begin to explain how to integrate Flash with XML, you need to understand better how the XML (eXtensible Markup Language).

Solution

Tutorial Flash + XML

Detailed explanation

  order better to understand the XML, let's examine this code: 

xml version = "1.0" encoding = "iso-8859-1"? 
<galeria> 
<Photo imagem="casinha.jpg" legenda="Minha casa"/> 
<Photo imagem="familia.jpg" legenda="Família reunida"/> 
<Photo imagem="reuniao.jpg" legenda="Reunião of negócios"/> 
<Photo imagem="cachorro.jpg" legenda="Meu cachorro"/> 
<Photo imagem="galera.jpg" legenda="Festa of galera"/> 
</ gallery> 
Note that the first line we declare the XML version and encoding that supports accents, so we can put accents on our tags. 

In the second line open with "galeria", stating that there begins the photo gallery. 

The 3rd to the 7th line are the photos and captions. For each picture you have to declare in this way. 

<image - to indicate the beginning of a new photo; 
image = "casinha.jpg" - to declare the location of the file; 
caption = "My home" - to inform the text to image; 
/> - Declares the end of the photo. 

Save your xml as "xml file" (without quotes) and go to Flash. 
To test the code, we use the trace to display the results in the output window. 

Open Flash and the 1st frame put this code: 
(The two bars after each line serve for comments on the flash.) 

System.useCodepage = true / / enables accents; 
var file: XML = new XML (); / / create a variable to read the xml; 
arquivo.load ( "xml file"); / / send the variable to read the xml file previously saved; 
arquivo.ignoreWhite = true / / ignore white space in XML 
arquivo.onLoad = function () (/ / after reading the XML, performs the action: 
trace (this); / / output in the Output window. 

Press Ctrl + Enter and see that appear throughout our XML in the output window. 
So far so easy, no? 

We will then "enter" on the lines of XML. 
To better explain, understand that the XML the count starts from 0, ie, line 1 is actually the line 0. 

Replace the code with the following: 

System.useCodepage = true; 
var file: XML = new XML (); 
arquivo.load ( "xml file"); 
arquivo.ignoreWhite = true; 
arquivo.onLoad = function () ( 
trace (this.childNodes [0]); 

Note that now he only showed that between <galeria> ... </ gallery>. 
But why? Simple, because we wanted to inform you that everything in the first node of the XML. ChildNodes Each represents a node in XML. 
You could have also used "firstChild" (without quotation marks). 

Let's get more in XML. 

System.useCodepage = true; 
var file: XML = new XML (); 
arquivo.load ( "xml file"); 
arquivo.ignoreWhite = true; 
arquivo.onLoad = function () ( 
trace (this.childNodes [0]. childNodes [0]); 

Thus it accesses the first row of photos. Remember that in XML, the count starts from 0. 

Your outbox should have appeared as follows: 

<Photo imagem="casinha.jpg" legenda="Minha casa" /> 
Okay, all right. 
When we use XML to read images and texts outside, we can not so right? We must speak what we want to attribute it take for it we use a loadMovie. 
So let's do it now. Take only the text and the name of the file in XML. 
As we are already in line will read the attributes of the picture, we can declare a "attributes" for him and ready. 

System.useCodepage = true; 
var file: XML = new XML (); 
arquivo.load ( "xml file"); 
arquivo.ignoreWhite = true; 
arquivo.onLoad = function () ( 
trace (this.childNodes [0]. childNodes [0]. attributes.image); 
trace (this.childNodes [0]. childNodes [0]. attributes.legenda); 

Note that we now have 2 trace. One for the photo and one for the caption. 
At first, the "attributes.image" gets the value that is declared in the xml as image = "casinha.jpg. And second, the "attributes.legenda" takes its caption in XML. 

Your output window should have been this: 

casinha.jpg 
My home 
Now to display the information on the stage, do the following: 

Create a movie clip, drag it to the stage and instantiate it "clip" (without quotation marks); 
Create a dynamic text field and instantiate it in "Legend" (without quotation marks); 
Now just tell the flash to read the image and text within the corresponding targets. To do this, paste the following code: 

System.useCodepage = true; 
var file: XML = new XML (); 
arquivo.load ( "xml file"); 
arquivo.ignoreWhite = true; 
arquivo.onLoad = function () ( 
clipe.loadMovie (this.childNodes [0]. childNodes [0]. attributes.image); 
legenda.text this.childNodes = [0]. childNodes [0]. attributes.legenda; 

Ready. Your movie clip instantiated as "clip" will read the image corresponding to the first line of XML, which in this case is "casinha.jpg" and its text box will read the corresponding legend. 

I hope you enjoyed and do not forget to always invent, create and test code, it will make you take practice and soon it will be a breeze for you.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes