Flash / Flex / ActionScript/XML/XML variable
Содержание
- 1 Change is made to the original document
- 2 " character can be used to delimit an attribute value in an XML literal, it is converted to the " character
- 3 Converting Strings to XML
- 4 Converting XML to Strings: The toString() method for XML objects will usually display the contents of an XML element including any tags.
- 5 Creates a variable refering to xml data child nodes
- 6 Creates the same XML hierarchy and dynamically specifies all element names, attribute names, attribute values, and element contents.
- 7 Invoke string method on XML data
- 8 Using + and += Operators to combine XML Nodes
Change is made to the original document
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);
var children:XMLList = novel.*;
trace(children);
delete novel.PUBLISHER;
trace(novel);
trace(children);
}
}
}
" character can be used to delimit an attribute value in an XML literal, it is converted to the " character
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var p:XML = <p align="left"/>;
trace(p.toXMLString( )); // Displays: <p align="left"/>
}
}
}
Converting Strings to XML
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var dialog:String = "Lorem ipsum dolor sit amet";
var XMLString:String = "<dialog>" + dialog + "</dialog>";
var xml:XML = XML(XMLString);
trace(xml.text()); // Displays: Lorem ipsum dolor sit amet
}
}
}
Converting XML to Strings: The toString() method for XML objects will usually display the contents of an XML element including any tags.
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);
}
}
}
Creates a variable refering to xml data child nodes
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK ISBN="0141182806">
<TITLE>Ulysses</TITLE>
<AUTHOR>Joyce, James</AUTHOR>
<PUBLISHER>Penguin Books Ltd</PUBLISHER>
</BOOK>
var children:XMLList = novel.*;
trace(children);
}
}
}
Creates the same XML hierarchy and dynamically specifies all element names, attribute names, attribute values, and element contents.
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);
}
}
}
Invoke string method on XML data
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK ISBN="0000000000" INSTOCK="false">
<TITLE>ActionScript</TITLE>
<AUTHOR>J, J</AUTHOR>
<PUBLISHER>Books Ltd</PUBLISHER>
</BOOK>;
if (novel.@INSTOCK.toLowerCase( ) == "false") {
trace("Not Available!");
} else {
trace("Available!");
}
}
}
}
Using + and += Operators to combine XML Nodes
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 anotherMovie:XML = <movie id="222">
<title>Tiger</title>
<year>1982</year>
<director>S L</director>
</movie>;
This may be the simplest way to add data to an XML structure.
movieList.movie += anotherMovie;
trace(movieList);
}
}
}