Flash / Flex / ActionScript/XML/Double dot
Содержание
- 1 pull all of the attributes from the entire XML document
- 2 The double-dot operator works for any level of nesting
- 3 Use the length( ) method to count the number of elements found: trace( items.item.length( ) );
- 4 Using the Descendant Accessor (..) to make a deep dive to the data without worrying about the path.
pull all of the attributes from the entire XML document
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var movieList:XML = <movieList> <listName>My favorite movies</listName> <movie id="123"> <title>Titus</title> <year>1999</year> <director>J T</director> </movie> <movie id="456"> <title>Rushmore</title> <year>1998</year> <director>W A</director> </movie> <movie id="789"> <title>Hall</title> <year>1977</year> <director>Woody Allen</director> </movie> </movieList>; trace(movieList..@*.toXMLString()); // Displays: 123 } }
}
</source>
The double-dot operator works for any level of nesting
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var author:XML = <author><name><firstName>Darron</firstName></name></author>; trace( author..firstName ); } }
}
</source>
Use the length( ) method to count the number of elements found: trace( items.item.length( ) );
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ // To examine every element node with a particular node name, use a for each loop var items:XML = <items> <item> <name>Apple</name> <color>red</color> </item> <item> <name>Orange</name> <color>orange</color> </item> </items>; for each ( var name:XML in items..name ) { trace( name ); } } }
}
</source>
Using the Descendant Accessor (..) to make a deep dive to the data without worrying about the path.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var movieList:XML = <movieList> <listName>My favorite movies</listName> <movie id="123"> <title>Titus</title> <year>1999</year> <director>J T</director> </movie> <movie id="456"> <title>Rushmore</title> <year>1998</year> <director>W A</director> </movie> <movie id="789"> <title>Hall</title> <year>1977</year> <director>Woody Allen</director> </movie> </movieList>; trace(movieList..title); } }
}
</source>