MAC
MAC

Reputation: 521

Flex, xml and E4x

I have a basic question. I am loading an XML file using the URLLoader and putting it into an XML variable.

My question is, can i leverage E4x to go through this xml data.

I tried doing

    for each (var grid in xmlData.grid){

        output.text=grid.name;

    }

But it says that variable 'grid' has no type declaration. This might make some sense since there is no way for the compiler to know before hand the structure of the XML that i am loading.

But since i am new to AS3 and flex, i was wondering if there is a way of leveraging E4x?

Thanks

Upvotes: 0

Views: 368

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82068

You could type it anonymously (it would solve the problem):

for each( var grid:* in xmlData.grid) {

but before you do that, consider these options here:

// NOTE: This is a for...in, not a for each...in
for (var grid:XML in xmlData.grid){

    // This will give you the node name: 
    // <foo/> returns (basically) "foo"
    output1.text=grid.name();

    // This will give you the node attribute called name: 
    // <foo name="bar"/> returns bar
    output2.text=grid.@name;

    // This will give you the child node named 'name': 
    // <foo><name>Heidi</name></foo> returns <name>Heidi</name>, which, 
    // when translated, should output "Heidi" as text
    output3.text=grid.name;
}

If you use one of those judiciously, it will probably be closer to what you're looking for.

Upvotes: 1

Related Questions