Flash / Flex / ActionScript/XML/loop through
Содержание
- 1 Accessing Descendants
- 2 Create Date object from xml data
- 3 Iterating through the Children of an Element
- 4 Processing XML with for-each-in and for-in
- 5 Tree traversal: converts every element and attribute name to lowercase:
- 6 Use for each loop to access the attributes
- 7 Use for loop to go through the xml data
- 8 Use the for..in or for each..in statements to iterate through an XMLList.
Accessing Descendants
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var loan:XML = <LOAN>
<BOOK ISBN="0000000000" DUE="1136091600000">
<TITLE>ActionScript</TITLE>
<AUTHOR>J, J</AUTHOR>
<PUBLISHER>Books Ltd</PUBLISHER>
</BOOK>
</LOAN>;
for each (var title:XML in loan..TITLE) {
trace(title);
}
}
}
}
Create Date object from xml data
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var loan:XML = <LOAN>
<BOOK ISBN="0000000000" DUE="1136091600000">
<TITLE>ActionScript</TITLE>
<AUTHOR>J, J</AUTHOR>
<PUBLISHER>Books Ltd</PUBLISHER>
</BOOK>
</LOAN>;
for each (var due:XML in loan..@DUE) {
trace(new Date(Number(due)));
}
}
}
}
Iterating through the Children of an Element
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>;
var movieTitles:Array = new Array();
var movies:XMLList = movieList.movie;
for (var i:int = 0; i < movies.length(); i++) {
movieTitles[i] = movies[i].title;
}
movieTitles.sort();
trace(movieTitles); // Displays: Annie Hall,Rushmore,Titus
}
}
}
Processing XML with for-each-in and for-in
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK ISBN="0000000000">
<TITLE>ActionScript</TITLE>
<AUTHOR>J, J</AUTHOR>
<PUBLISHER>Books Ltd</PUBLISHER>
</BOOK>;
for each (var child:XML in novel.*) {
trace(child);
}
}
}
}
Tree traversal: converts every element and attribute name to lowercase:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK ISBN="0000000000">
<TITLE>ActionScript</TITLE> <!--Previous sibling-->
<AUTHOR>J, J</AUTHOR>
<PUBLISHER>Books Ltd</PUBLISHER> <!--Next sibling-->
</BOOK>;
for each (var child:XML in novel..*) {
if (child.nodeKind( ) == "element") {
child.setName(child.name().toString().toLowerCase( ));
for each (var attribute:XML in child.@*) {
attribute.setName(attribute.name().toString().toLowerCase( ));
}
}
}
}
}
}
Use for each loop to access the attributes
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var loan:XML = <LOAN>
<BOOK ISBN="0000000000" DUE="111111111111111111">
<TITLE>ActionScript</TITLE>
<AUTHOR>J, J</AUTHOR>
<PUBLISHER>Books Ltd</PUBLISHER>
</BOOK>
</LOAN>;
for each (var attribute:XML in loan..@*) {
trace(attribute.parent( ).TITLE
+ ": " + attribute.name( ) + "=" + attribute);
}
}
}
}
Use for loop to go through the xml data
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var order:XML = <ORDER>
<ITEM SKU="374">
<NAME>Toy</NAME>
<PRICE>39.99</PRICE>
<QUANTITY>2</QUANTITY>
</ITEM>
</ORDER>
for (var childName:String in order.*) {
trace(childName);
}
}
}
}
Use the for..in or for each..in statements to iterate through an XMLList.
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>;
var movies:XMLList = movieList.movie;
for each (var movie:XML in movies) {
movie.title = movie.title.toUpperCase();
}
trace(movies.title);
}
}
}