Flash / Flex / ActionScript/XML/child — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:14, 26 мая 2010
Содержание
- 1 Access child nodes using E4X"s more convenient properties wildcard (*).
- 2 appendChild to XML value
- 3 Delete all children contained by an element
- 4 Get tag child
- 5 Get the first child in a list of child nodes
- 6 Get the last child in a list of child nodes: theNode.children( )[theNode.children().length( )-1]
- 7 prependChild, insertChildAfter, insertChildBefore
- 8 Removes one element from its parent element
- 9 Replacing Values in XML Nodes: the setChildren() method.
- 10 Returns all child elements of BOOK named AUTHOR
- 11 Use insertChildBefore( ) and insertChildAfter( ) to modify an XML tree
- 12 Using child(), you can search the element for any children with a given name.
- 13 Using XML Methods to Combine Values
- 14 You can also use the children() method to get a child by index rather than node name.
Access child nodes using E4X"s more convenient properties wildcard (*).
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var rootElementName:String = "BOOK";
var rootAttributeName:String = "ISBN";
var childElementNames:Array = ["TITLE", "AUTHOR", "PUBLISHER"];
var bookISBN:String = "0000000000";
var bookTitle:String = "ActionScript";
var bookAuthor:String = "J, J";
var bookPublisher:String = "Books Ltd";
var novel:XML = <{rootElementName} {rootAttributeName}={bookISBN}>
<{childElementNames[0]}>{bookTitle}</{childElementNames[0]}>
<{childElementNames[1]}>{bookAuthor}</{childElementNames[1]}>
<{childElementNames[2]}>{bookPublisher}</{childElementNames[2]}>
</{rootElementName}>;
trace(novel);
}
}
}
appendChild to XML value
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:XML = <example/>;
example.appendChild( <two>2</two> );
example.prependChild( <one>"number 1"</one> );
example.insertChildAfter( example.one[0], 1.5 );
example.insertChildBefore( example.two[0], <part>1.75</part> );
trace( example );
}
}
}
Delete all children contained by an element
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);
}
}
}
Get tag child
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var rootElementName:String = "BOOK";
var rootAttributeName:String = "ISBN";
var childElementNames:Array = ["TITLE", "AUTHOR", "PUBLISHER"];
var bookISBN:String = "0000000000";
var bookTitle:String = "ActionScript";
var bookAuthor:String = "J, J";
var bookPublisher:String = "Books Ltd";
var novel:XML = <{rootElementName} {rootAttributeName}={bookISBN}>
<{childElementNames[0]}>{bookTitle}</{childElementNames[0]}>
<{childElementNames[1]}>{bookAuthor}</{childElementNames[1]}>
<{childElementNames[2]}>{bookPublisher}</{childElementNames[2]}>
</{rootElementName}>;
trace(novel.children());
}
}
}
Get the first child in a list of child nodes
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var rootElementName:String = "BOOK";
var rootAttributeName:String = "ISBN";
var childElementNames:Array = ["TITLE", "AUTHOR", "PUBLISHER"];
var bookISBN:String = "0000000000";
var bookTitle:String = "ActionScript";
var bookAuthor:String = "J, J";
var bookPublisher:String = "Books Ltd";
var novel:XML = <{rootElementName} {rootAttributeName}={bookISBN}>
<{childElementNames[0]}>{bookTitle}</{childElementNames[0]}>
<{childElementNames[1]}>{bookAuthor}</{childElementNames[1]}>
<{childElementNames[2]}>{bookPublisher}</{childElementNames[2]}>
</{rootElementName}>;
trace(novel.children()[0]);
}
}
}
Get the last child in a list of child nodes: theNode.children( )[theNode.children().length( )-1]
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var rootElementName:String = "BOOK";
var rootAttributeName:String = "ISBN";
var childElementNames:Array = ["TITLE", "AUTHOR", "PUBLISHER"];
var bookISBN:String = "0000000000";
var bookTitle:String = "ActionScript";
var bookAuthor:String = "J, J";
var bookPublisher:String = "Books Ltd";
var novel:XML = <{rootElementName} {rootAttributeName}={bookISBN}>
<{childElementNames[0]}>{bookTitle}</{childElementNames[0]}>
<{childElementNames[1]}>{bookAuthor}</{childElementNames[1]}>
<{childElementNames[2]}>{bookPublisher}</{childElementNames[2]}>
</{rootElementName}>;
trace(novel.children( )[novel.children().length( )-1]);
}
}
}
prependChild, insertChildAfter, insertChildBefore
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:XML = <example>
<b />
<d />
<f />
</example>;
example.appendChild(<g />);
trace(example);
example.prependChild(<a />);
trace(example);
example.insertChildAfter(example.b, <c />);
trace(example);
example.insertChildBefore(example.f, <e />);
trace(example);
}
}
}
Removes one element from its parent element
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.TITLE;
trace(novel);
}
}
}
Replacing Values in XML Nodes: the setChildren() method.
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>;
movieList.movie.(@id == 123).setChildren(null);
trace(movieList.movie.(@id == 222).toXMLString());
movieList.movie.(@id == 123).setChildren(<title>Sleep</title> +
<year>2006</year> +
<director>M G</director> +
<genre>Romance</genre>);
trace(movieList.movie.(@id == 123).toXMLString());
}
}
}
Returns all child elements of BOOK named AUTHOR
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var rootElementName:String = "BOOK";
var rootAttributeName:String = "ISBN";
var childElementNames:Array = ["TITLE", "AUTHOR", "PUBLISHER"];
var bookISBN:String = "0000000000";
var bookTitle:String = "ActionScript";
var bookAuthor:String = "J, J";
var bookPublisher:String = "Books Ltd";
var novel:XML = <{rootElementName} {rootAttributeName}={bookISBN}>
<{childElementNames[0]}>{bookTitle}</{childElementNames[0]}>
<{childElementNames[1]}>{bookAuthor}</{childElementNames[1]}>
<{childElementNames[2]}>{bookPublisher}</{childElementNames[2]}>
</{rootElementName}>;
trace(novel.AUTHOR); //
}
}
}
Use insertChildBefore( ) and insertChildAfter( ) to modify an XML tree
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var example:XML = <example/>;
example.two = "";
// Before the two element node, add a one element node
example = example.insertChildBefore( example.two, <one /> );
// After the two element node, add a three element node
example = example.insertChildAfter( example.two, <three /> );
trace( example );
}
}
}
Using child(), you can search the element for any children with a given name.
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.child("year"));
}
}
}
Using XML Methods to Combine Values
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>;
movieList.movie[0].appendChild(<genre>Drama</genre>);
movieList.movie[1].appendChild(<genre>Comedy</genre>);
movieList.movie[2].appendChild(<genre>Sci-Fi</genre>);
trace(movieList.movie);
}
}
}
You can also use the children() method to get a child by index rather than node name.
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.children()[0].toXMLString());
trace(movieList.children()[2].children()[0].toXMLString());
}
}
}