Flash / Flex / ActionScript/XML/attributes
Содержание
- 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 (@*)
<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.@*); } }
}
</source>
Accesses first attribute by using E4X"s attributes wildcard syntax
<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.@*[0]); } }
}
</source>
Accessing Attributes and Elements Whose Names Contain Reserved Characters: attribute( ), child( ), or [] operator.
<source lang="java">
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"]); } }
}
</source>
Adding New Attributes and Elements
<source lang="java">
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); } }
}
</source>
Assign the desired attributes the desired values
<source lang="java">
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); } }
}
</source>
Attributes in an XMLList can be accessed using the array-access operator ([]).
<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.attributes()[0]); } }
}
</source>
Changing an Attribute Value
<source lang="java">
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> <AUTHOR>J, J</AUTHOR> <PUBLISHER>Books Ltd</PUBLISHER> </BOOK>; novel.@ISBN = "0684800713"; trace(novel); } }
}
</source>
Returns an XMLList that contains one XML instance
<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.attribute("ISBN")); } }
}
</source>
Returns an XMLList that contains one XML instance, representing the attribute, with variable-access syntax.
<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.@ISBN); } }
}
</source>
To access an element"s attributes: attributes()
<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.attributes()); } }
}
</source>
To access the XML instance contained by the XMLList returned by novel.@ISBN
<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.@ISBN[0]); } }
}
</source>
To loop over all of the attributes of an element, use a for each loop.
<source lang="java">
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( ) ); } } }
}
</source>
Unusual behavior can be used to collect a group of attributes into a single attribute
<source lang="java">
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); } }
}
</source>
Use the name( ) method on the attribute reference to retrieve its name
<source lang="java">
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( ) );
} }
}
</source>