Working with XML in flex can be a headache, but using the so-called E4X is muitoooo easy.
The E4X (ECMAScript for XML) is a "Bookshop" added the Action Script 3 to natively work with you XML and are intended to simplify access to files / XML content. Using E4X can access the items of xml as if they were objects in Action Script.
I saw some tutorials for the net, but or are incomplete or are too complex ... so got to work and decided to write a tutorial so they can better understand in a clear and Portuguese.
Let us begin ...
If someone has worked with xml form of "normal / old" knows how things could be complicated, however, were complicated by nature mainly with the methods. FirstChild and childNodes in xml that's more complicated was simply a mess to work with themselves and detect errors in the code ...
Take the following XML code:
<Article>
</ nome > <Name> Tennis Nike </ name>
</ modelo > <Model> 90 </ type>
</ Item>
<Article>
</ nome > <Name> Tennis Puma </ name>
</ modelo > <Model> Sport fun </ type>
</ Item>
<Article>
</ nome > <Name> Tennis Adidas </ name>
</ modelo > <Model> AD 215 </ type>
</ Item>
</ Store>
This is a kind of a simple XML, which can be represented in actionscript like this:
Very simple so far ...
Using E4X, we can show the contents of our shares by MXL, see:
is each (var item in artigo ){ meuXML article ..) (
" Artigo: " + artigo . nome + " do modelo " + artigo . modelo ) ; trace ( "Rule:" + article. + name "of type" + article. model);
)
)
/ / or we can do even more simply:
" artigos " + meuXML .. artigo .* ) ; trace ( "Articles" + meuXML .. article .*);
And so we go our XML and list the items ...
" artigo 1: " + meuXML .. artigo [ 1 ]) ; trace ( "Rule 1:" + meuXML .. [1]);
/ / list the name and model in XML format
" nome artigo 1: " + meuXML .. artigo [ 1 ] . nome . text ()) ; trace ( "name Article 1:" + meuXML .. [1]. name. text ());
/ / list the contents of the first article
" modelo artigo 1: " + meuXML . artigo [ 1 ] . modelo . text ()) ; trace ( "model Article 1:" + meuXML. [1]. template. text ());
/ / list the contents of the first article
" id do artigo 1: " + meuXML .. artigo [ 1 ] .@ id ) ; trace ( "id Article 1:" + meuXML .. [1]. @ id);
/ / list the id = "" Article 1
" procurar infos do artigo com id=2 " + meuXML .. artigo . ( @ id == " 2 " ) .* ) ; trace ( "Infos seek the article with id = 2" + meuXML article ... (@ id == "2") .*);
/ / xml list in the name and model of Article 2 by making a demand
" procurar nome do artigo com id=2 " + meuXML .. artigo . ( @ id == " 2 " ) . nome . text ()) ; trace ( "look behalf of the article with id = 2" + meuXML article ... (@ id == "2"). name. text ());
/ / list the name of the article with id = "2";
" procurar modelo do artigo com id=2 " + meuXML .. artigo . ( @ id == " 2 " ) . modelo . text ()) ; trace ( "search model of the article with id = 2" + meuXML article ... (@ id == "2"). template. text ());
/ / list the model of the article with id = "2";
Very simple ah?
And ready, you can see how simple it is working with xml ... I will give an example of assigning the meuXML an external XML file:
private var XML ; meuXML: XML;
private var URLLoader ; loader: URLLoader;
private function : void carregaXML (): void (
new loader = new ; URLLoader ();
addEventListener ( Event . COMPLETE , onComplete ) ; loader. addEventListener (Event. COMPLETE, onComplete);
load ( new loader. load (new " http://www.meusite.com/meu_ficheiro_xml.xml " )) ; URLRequest ( "http://www.meusite.com/meu_ficheiro_xml.xml"));
)
private function evt : Event ) : void onComplete (evt: Event): void
(
new meuXML = new loader . data ) ; XML (loader. Date);
/ / and ready, the silver here are the contents of the XML variable and may use the operation as outlined above.
)
To load the xml, just in your application using flex trigger creationComplete = "carregaXML ()"
... How come there is no excuse for not working with XML using E4X!
I hope it will be useful
Hug.













A Comment
good example, although deberias poner un BAJAR that can link to the example and you could try, nothing better than the practice.
salu2.