Search code examples
actionscript-3apache-flexactionscripte4x

Access XML Child Nodes Dynamically E4X ActionScript 3


I want to create a utility function that requires accessing XML children nodes dynamically.

Sample XML:

var xml:XML = 
<root>
    <section>
        <lt target='foo'/>
        <lt target='foo1'/>
        <lt target='foo2'/>
    </section>
    <section1>
        <lt target='foo'/>
        <lt target='foo1'/>
        <lt target='foo2'/>
    </section1>
</root>;

I want to be able to access all the 'lt' nodes regardless of its parent node. Normally, you would do that like this:

var xList:XMLList = xml..lt;

//Output

xList = 
<lt target='foo'/>
<lt target='foo1'/>
<lt target='foo2'/>
<lt target='foo'/>
<lt target='foo1'/>
<lt target='foo2'/>

That works fine, however, I need to access the 'lt' node not knowing the name up front. For instance...

var nodeName:String = 'lt';
var xList:XMLList = xml..[nodeName]; //<-- Does not work.

I was hoping to get this done without using a for loop. Any ideas?

Thanks,

Victor


Solution

  • You probably just need:

     var nodeName:String = "lt";
     var xList:XMLList = xml.descendants( nodeName );