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

StackOverflow https://stackoverflow.com/questions/7136475

문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top