开发者

How many levels deep is the e4x implementation using square bracket array access notation in AS3?

With bracket notation I can access a开发者_开发百科 direct child node by name or attribute using the following code:

Example XML:

<item name="item1">
   <categories name="catList">
      <category name="cat1">
   </categories>
</item>

Example accessing direct child node:

trace(xml["categories"].toString()); // <categories><category/></categories> 

Example accessing node attribute:

trace(xml["@name"].toString()); // item1

Updated: Is there a way to access a subelement / nested element / nested attribute using only a single square bracket notation?

For example,

trace(xml["categories.category.@name"].toString()); // cat1

or

trace(xml["categories.@name"]); // catList


Using the square bracket notation is unnecessary if you know the names of the nodes you're accessing. This should work fine:

xml.categories.category.@name.toString();

Square bracket notation is used to access properties with a string name. It is not specifically related to E4X. The translation of all properties to square bracket notation would be this:

xml['categories']['category']['@name']['toString']();


You can do something like this:

xml["categories"]["category"][0]["@name"].toString();

The XML class implements a Proxy-like interface. The dynamic properties are dynamically resolved when used, which is why you can invoke properties that aren't defined explicitly on the XML class. However, if you are doing some dynamic XML name stuff, and need to use the square bracket notation with strings, then you can definately do that. You just have to wrap each one in its own [ ].

However, there are probably things that you can do with e4x notation that you can't do with [ ]. Namely doing things like:

xml..@name

Which will find all name attributes in the entire xml tree. I dont think there is any other way to represent that.


Based on your comments to other answers it seems as though this rather unfortunate use of e4x is deep in the Flex SDK and not something you can alter. In that case I would have to say I'm sorry but you're likely out of luck. Unless of course you own the 'a' variable and can pass that on, in which case you could do the selection beforehand and pass on the appropriate xml fragment to whatever function it is you're calling.

Out of curiosity (and in order to possibly give a better answer,) where in the Flex SDK is this?


I had a look at SortField as per your comments and while it does seem cumbersome (and somewhat dangerous) to abuse the internals of the xmlCompare method you should be able to work around it by supplying a custom compareFunction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜