Flash / Flex / ActionScript/XML/update
Содержание
- 1 After the replacement, the two <PASSWORD> elements have the same content:
- 2 Changing the Contents of an Element
- 3 Content of an element can be changed using setChildren( )
- 4 Get and set the xml tag value
- 5 Replace the element of one xml data with the PASSWORD element of another
- 6 Set attribute values
- 7 The content of an element can be changed using the XML class"s instance method replace( ).
- 8 Update xml data by using the dynamic property
- 9 user2.PASSWORD[0]); // Displays: false
- 10 Use wild card and index to change the xml data value
- 11 Write new values to the XML
After the replacement, the two <PASSWORD> elements have the same content:
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var user1:XML = <USERDETAILS> <LOGIN>joe</LOGIN> <PASSWORD>linuxRules</PASSWORD> </USERDETAILS>; var user2:XML = <USERDETAILS> <LOGIN>ken</LOGIN> <PASSWORD>default</PASSWORD> </USERDETAILS>; trace(user1); trace(user2); user2.PASSWORD = user1.PASSWORD; trace(user1); trace(user2); trace(user1.PASSWORD[0] == user2.PASSWORD[0]); // Displays: true trace(user1.PASSWORD[0] </source>
Changing the Contents of an Element
<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> novel.TITLE[0] = "New Title"; trace(novel); } }
}
</source>
Content of an element can be changed using setChildren( )
<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); novel.TITLE.setChildren("New Title"); trace(novel); } }
}
</source>
Get and set the xml tag value
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var order:XML = <ORDER> <ITEM SKU="374"> <NAME>Toy</NAME> <PRICE>39.99</PRICE> <QUANTITY>2</QUANTITY> </ITEM> </ORDER> var total:Number = 0; for each (var item:XML in order.*) { trace( item.QUANTITY + " " + item.NAME + "(s)." + " $" + item.PRICE + " each.\n"); total += item.QUANTITY * item.PRICE; } trace("TOTAL: " + total); for each (var item:XML in order.*) { item.PRICE = 1; } trace(order); } }
}
</source>
Replace the element of one xml data with the PASSWORD element of another
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var user1:XML = <USERDETAILS> <LOGIN>joe</LOGIN> <PASSWORD>linuxRules</PASSWORD> </USERDETAILS>; var user2:XML = <USERDETAILS> <LOGIN>ken</LOGIN> <PASSWORD>default</PASSWORD> </USERDETAILS>; trace(user1); trace(user2); user2.PASSWORD = user1.PASSWORD; trace(user1); trace(user2); } }
}
</source>
Set attribute values
<source lang="java">
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].@id = 8675309; } }
}
</source>
The content of an element can be changed using the XML class"s instance method replace( ).
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var doc:XML = <DOC>
E4X is fun
</DOC>;doc.replace("P",
E4X is convenient
);
trace(doc); } }
}
</source>
Update xml data by using the dynamic property
<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); novel.AUTHOR = "H, E"; novel.PUBLISHER = "S"; trace(novel); } }
}
</source>
user2.PASSWORD[0]); // Displays: false
<source lang="java"> } }
}
</source>
Use wild card and index to change the xml data value
<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> novel.*[1] = novel.*[1] + <AUTHOR>D, V</AUTHOR>; trace(novel); } }
}
</source>
Write new values to the XML
<source lang="java">
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[2].director = "A K"; } }
}
</source>