views:

111

answers:

1

Hi

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

+1  A: 

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.

Christopher W. Allen-Poole
Thanks worked like a charm.I guess the only issue was that i was not giving var grid a type. Funny though the tutorial i was looking at did not give one either. But whatever works!
MAC
@MAC - I've found that a lot of online tutorials and examples for flex seem to be written without being tested. They often leave out types, "var" keyword, and even call a function different than the name of the function in their own example. What I'm trying to say is take them with a grain of salt. Those tutorials have the right idea as far as technique to follow, but can really mess you up with bad syntax
invertedSpear