Flash / Flex / ActionScript/XML/XMLList — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:14, 26 мая 2010
Содержание
- 1 Assigning Values to an XMLList
- 2 Converting XMLList to a String: toString( ) had been invoked on the single XML instance directly:
- 3 To access a specific child in an XMLList
- 4 To access the XMLList representing child nodes, use the XML class"s instance method children( ).
- 5 To replace an XML element with new elements, we assign either an XMLList or XML object to that element
- 6 Treating XMLList as XML
Assigning Values to an XMLList
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var doc:XML = <DOC TOPIC="Code Tips" AUTHOR="Colin">
<P>Errors are your friends</P>
<P>Backup often</P>
</DOC>;
doc.* = <P>Practice coding everyday</P>;
trace(doc.*);
}
}
}
Converting XMLList to a String: toString( ) had been invoked on the single XML instance directly:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK ISBN="0141182806">
<TITLE>Actionscript</TITLE>
<AUTHOR>J, K</AUTHOR>
<PUBLISHER>Penguin Books Ltd</PUBLISHER>
</BOOK>
var title:XMLList = novel.TITLE;
trace(title); // Displays: Actionscript
var details:XMLList = novel.*;
trace(details);
}
}
}
To access a specific child in an XMLList
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( )[1]); // A reference to <BOOK>"s second child node
//or:
trace(novel.*[1]); // Also a reference to <BOOK>"s second child node
}
}
}
To access the XMLList representing child nodes, use the XML class"s instance method children( ).
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.*);
}
}
}
To replace an XML element with new elements, we assign either an XMLList or XML object to that element
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var doc:XML = <DOC>
<P ALIGN="CENTER">E4X is fun</P>
</DOC>;
doc.P = <DIV>E4X is convenient</DIV>;
trace(doc.P);
}
}
}
Treating XMLList as XML
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var oopBook:XML = <BOOK ISBN="000000000">
<TITLE>Design Patterns</TITLE>
<AUTHOR>
<NAME>Bates</NAME>
<BIRTHDATE>1 1973</BIRTHDATE>
</AUTHOR>
<PUBLISHER>Inc</PUBLISHER>
</BOOK>;
displayAuthor(oopBook.children());
}
public function displayAuthor (authors:XMLList):void {
for each (var author:XML in authors) {
trace( "Name: " + author.NAME
+ ", Birthdate: " + author.BIRTHDATE + "\n");
}
}
}
}