Flash / Flex / ActionScript/XML/attributes — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:14, 26 мая 2010
Содержание
- 1 Access an element"s attributes using E4X attributes wildcard (@*)
- 2 Accesses first attribute by using E4X"s attributes wildcard syntax
- 3 Accessing Attributes and Elements Whose Names Contain Reserved Characters: attribute( ), child( ), or [] operator.
- 4 Adding New Attributes and Elements
- 5 Assign the desired attributes the desired values
- 6 Attributes in an XMLList can be accessed using the array-access operator ([]).
- 7 Changing an Attribute Value
- 8 Returns an XMLList that contains one XML instance
- 9 Returns an XMLList that contains one XML instance, representing the attribute, with variable-access syntax.
- 10 To access an element"s attributes: attributes()
- 11 To access the XML instance contained by the XMLList returned by novel.@ISBN
- 12 To loop over all of the attributes of an element, use a for each loop.
- 13 Unusual behavior can be used to collect a group of attributes into a single attribute
- 14 Use the name( ) method on the attribute reference to retrieve its name
Access an element"s attributes using E4X attributes wildcard (@*)
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>;
trace(novel.@*);
}
}
}
Accesses first attribute by using E4X"s attributes wildcard syntax
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>;
trace(novel.@*[0]);
}
}
}
Accessing Attributes and Elements Whose Names Contain Reserved Characters: attribute( ), child( ), or [] operator.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var saleEndsDate:XML = <DATE TIME-ZONE="PST">1, 2006</DATE>
//trace(saleEndsDate.@TIME-ZONE);
trace(saleEndsDate.attribute("TIME-ZONE"));
trace(saleEndsDate.@["TIME-ZONE"]);
}
}
}
Adding New Attributes and Elements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK/>;
novel.@ISBN = "0000000000";
novel.TITLE = "ActionScript";
novel.AUTHOR = "J, J";
novel.PUBLISHER = "Books Ltd";
novel.SETTING.CITY = "Dublin";
novel.SETTING.COUNTRY = "Ireland";
trace(novel);
}
}
}
Assign the desired attributes the desired values
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var novel:XML = <BOOK/>;
novel.@ISBN = "0000000000";
novel.TITLE = "ActionScript";
novel.AUTHOR = "J, J";
novel.PUBLISHER = "Books Ltd";
novel.SETTING.CITY = "Dublin";
novel.SETTING.COUNTRY = "Ireland";
novel.SETTING.@CITY = "Dublin";
novel.SETTING.@COUNTRY = "Ireland";
trace(novel);
}
}
}
Attributes in an XMLList can be accessed using the array-access operator ([]).
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>;
trace(novel.attributes()[0]);
}
}
}
Changing an Attribute Value
To change an XML attribute value, simply assign the attribute any new value in an assignment statement.
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>;
novel.@ISBN = "0684800713";
trace(novel);
}
}
}
Returns an XMLList that contains one XML instance
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>;
trace(novel.attribute("ISBN"));
}
}
}
Returns an XMLList that contains one XML instance, representing the attribute, with variable-access syntax.
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>;
trace(novel.@ISBN);
}
}
}
To access an element"s attributes: attributes()
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>;
trace(novel.attributes());
}
}
}
To access the XML instance contained by the XMLList returned by novel.@ISBN
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>;
trace(novel.@ISBN[0]);
}
}
}
To loop over all of the attributes of an element, use a for each loop.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var fruit:XML = <fruit name="Apple" color="red" />;
for each ( var attribute:XML in fruit.attributes( ) ) {
trace( attribute.name( ) + " = " + attribute.toString( ) );
}
}
}
}
Unusual behavior can be used to collect a group of attributes into a single attribute
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var books:XML = <BOOKS>
<BOOK ISBN="0000000000"/>
<BOOK ISBN="1111111111"/>
<BOOK ISBN="2222222222"/>
</BOOKS>;
var order:XML = <ORDER ITEMS=""/>;
order.@ITEMS = books.*.@ISBN;
trace(order);
}
}
}
Use the name( ) method on the attribute reference to retrieve its name
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var fruit:XML = <fruit name="Apple" color="red" />;
trace( fruit.attributes( )[1].name( ) );
}
}
}