Flex tutorial - XML / E4X


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:

<Shop>
<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:

private var XML =< loja >< artigo >< nome > Tenis meuXML: XML = <shop> <Article> <name> Tennis / nome><modelo>90< / modelo >< / artigo><artigo><nome>Tenis Puma< / nome >< modelo > Sport Nike </ name> <modelo> 90 </ template> </ item> <artigo> <name> Tennis Puma </ name> <template> Sport / modelo>< / artigo >< artigo >< nome > Tenis fun </ template> </ item> <Article> <name> Tennis / nome><modelo>AD 215< / modelo >< / artigo>< / loja >; Adidas </ name> <modelo> AD 215 </ template> </ item> </ store>;

Very simple so far ...

Using E4X, we can show the contents of our shares by MXL, see:

private function : void { show (): void (
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 ...

Some Examples of reading and demand No XML:
" 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:

import net . URLLoader flash. net. URLLoader
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.

pdf version temporarily unavailable.
Leave a comment or a Trackback

A Comment

  1. Hiram Galindo
    October 4, 2008 at 9:52 pm | Permalink

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

3 Trackbacks

  1. By Flex with Rails + RubyAMF 'ramalho.wordpress.com on September 18, 2008 at 12:56 p.m. pm

    [...] The GET method of the HTTP protocol. The result - which we know is in XML - will be handled by E4X, which is a library of native access to XML. When the service comes into action, the cursor will [...]

  2. By User links about "e4x" on iLinkShare on September 28, 2008 at 12:03 p.m. pm

    [...] | User-saved public links | iLinkShare 3 votesTitorial Flex - XML / E4X>> saved by humeid 1 days ago3 votes [eactif] AS3 E4X All U need>> saved by interactive 7 days ago4 [...]

  3. For Recent Links Tagged With "e4x" - JabberTags on October 25, 2008 at 11:34 a.m. am

    [...] By Zackarito on Sat 25-10-2008 10 Reasons We Love Flex Saved by wisenheimer on Sat 25-10-2008 Titorial Flex - XML / E4X Saved by kristenjanell on Tue 24-10-2008 Flex, flex learn in Saved by a week HeartofRoxas [...]

Leave a comment

Your email is never published or shared. Required fields are marked with an *