Flash / Flex / ActionScript/XML/delete
Содержание
- 1 Delete all attributes of an element
- 2 Remove all attributes from the movie Tron.
- 3 Removing Elements, Text Nodes, and Attributes
- 4 Removing XML Nodes
- 5 The delete operator works for other types of nodes as well, such as attributes.
- 6 To delete all the items in the XMLList, use a for loop and iterate over the items in reverse order
Delete all attributes of an element
<source lang="java">
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>; trace(novel); delete novel.@*; trace(novel); } }
}
</source>
Remove all attributes from the movie Tron.
<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>; var tron:XML = movieList.movie[2]; delete tron.@*; trace(tron); tron.id = 222; // Adding the id attribute back. } }
}
</source>
Removing Elements, Text Nodes, and Attributes
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var example:XML = <example> <fruit color="Red">Apple</fruit> <vegetable color="Green">Broccoli</vegetable> <dairy color="White">Milk</dairy> </example>; delete example.fruit.@color; delete example.dairy; delete example.vegetable.text( )[0]; trace( example );
} }
}
</source>
Removing XML Nodes
<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>; delete movieList.movie[2]; trace(movieList.movie); } }
}
</source>
The delete operator works for other types of nodes as well, such as attributes.
<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>; var tron:XML = movieList.movie[2]; delete tron.@*; trace(tron); tron.id = 222; // Adding the id attribute back. trace(tron); } }
}
</source>
To delete all the items in the XMLList, use a for loop and iterate over the items in reverse order
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var example:XML = <example> <fruit color="red" name="Apple" /> </example>; var attributes:XMLList = example.fruit.@*; for ( var i:int = attributes.length( ) - 1; i >= 0; i-- ) { delete attributes[i]; } trace( example ); } }
}
</source>