views:

151

answers:

3

Is there any way to concatenate E4X expressions? If I store "half" a path in a variable, can I concatenate the way I do with Strings?

A: 

You can always use array access by converting the E4X string into an array, seperated by the dots.

var xml:XML = 
    <data>
        <a><b><c><x><y><z attr="This is an attribute">This is a text!</z></y></x></c></b></a>
    </data>

var path1:String = "a.b.c";
var path2:String = "x.y.z";
var path3:String = "@attr";
var path:Array = ([path1, path2, path3].join('.')).split('.');

var result:XMLList = new XMLList(xml);

for (var i:uint = 0; i < path.length; i++) {
    result = result[path[i]];
}

trace(result); // This is an attribute
sunmaster13
A: 

Hi sunmaster13,

Your solution works great. I am wondering if this is applicable to all dot/array access in E4X? For example, how does it apply to filtering? Say if I want to get all the nodes:

result.item.(@id==1))

Can I do it using the approach you described?

smallbug