![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
The methods of the ActionScript XML class (for example, appendChild()
, removeNode()
, and insertBefore()
) let you structure XML data in Flash to send to a server and manipulate and interpret downloaded XML data.
The following XML class methods send and load XML data to a server by using the HTTP POST
method:
load()
method downloads XML from a URL and places it in an ActionScript XML object.send()
method passes an XML object to a URL. Any returned information is sent to another browser window.sendAndLoad()
method sends an XML object to a URL. Any returned information is placed in an ActionScript XML object.For example, you could create a brokerage system that stores all its information (user names, passwords, session IDs, portfolio holdings, and transaction information) in a database.
The server-side script that passes information between Flash and the database reads and writes the data in XML format. You can use ActionScript to convert information collected in the SWF file (for example, a user name and password) to an XML object and then send the data to the server-side script as an XML document. You can also use ActionScript to load the XML document that the server returns into an XML object to be used in the SWF file.
The flow and conversion of data between a Flash movie, a server-side script, and a database
The password validation for the brokerage system requires two scripts: a function defined on Frame 1, and a script that creates and sends the XML objects attached to the Submit button in the form.
When users enter their information into text fields in the SWF file with the variables username
and password
, the variables must be converted to XML before being passed to the server. The first section of the script loads the variables into a newly created XML object called loginXML
. When a user clicks the Submit button, the loginXML
object is converted to a string of XML and sent to the server.
The following script is attached to the Submit button. To understand this script, read the commented lines (indicated by the characters //)
:
on (release) { // A. Construct an XML document with a LOGIN element loginXML = new XML(); loginElement = loginXML.createElement("LOGIN"); loginElement.attributes.username = username; loginElement.attributes.password = password; loginXML.appendChild(loginElement); // B. Construct an XML object to hold the server's reply loginReplyXML = new XML(); loginReplyXML.onLoad = onLoginReply; // C. Send the LOGIN element to the server, // place the reply in loginReplyXML loginXML.sendAndLoad("https://www.imexstocks.com/main.cgi", loginReplyXML); }
The first section of the script generates the following XML when the user clicks the Submit button:
<LOGIN USERNAME="JeanSmith" PASSWORD="VerySecret" />
The server receives the XML, generates an XML response, and sends it back to the SWF file. If the password is accepted, the server responds with the following:
<LOGINREPLY STATUS="OK" SESSION="rnr6f7vkj2oe14m7jkkycilb" />
This XML includes a SESSION
attribute that contains a unique, randomly generated session ID, which will be used in all communications between the client and server for the rest of the session. If the password is rejected, the server responds with the following message:
<LOGINREPLY STATUS="FAILED" />
The LOGINREPLY
XML node must load into a blank XML object in the SWF file. The following statement creates the XML object loginreplyXML
to receive the XML node:
// B. Construct an XML object to hold the server's reply loginReplyXML = new XML(); loginReplyXML.onLoad = onLoginReply;
The second statement assigns the onLoginReply()
function to the loginReplyXML.onLoad
handler.
The LOGINREPLY
XML element arrives asynchronously, much like the data from a loadVariables()
function, and loads into the loginReplyXML
object. When the data arrives, the onLoad
handler of the loginReplyXML
object is called. You must define the onLoginReply()
function and assign it to the loginReplyXML.onLoad
handler so that it can process the LOGINREPLY
element. You must also assign the onLoginReply()
function to the frame that contains the Submit button.
The onLoginReply()
function is defined in the first frame of the SWF file. (To understand this script, read the commented lines.)
function onLoginReply() { // Get the first XML element var e = this.firstChild; // If the first XML element is a LOGINREPLY element with // status OK, go to the portfolio screen. Otherwise, // go to the login failure screen and let the user try again. if (e.nodeName == "LOGINREPLY" && e.attributes.status == "OK") { // Save the session ID for future communications with server sessionID = e.attributes.session; // Go to the portfolio viewing screen gotoAndStop("portfolioView"); } else { // Login failed! Go to the login failure screen. gotoAndStop("loginFailed"); } }
The first line of this function, var e = this.firstChild
, uses the keyword this
to refer to the XML object loginReplyXML
that has just been loaded with XML from the server. You can use this
because onLoginReply()
has been invoked as loginReplyXML.onLoad
, so even though onLoginReply()
appears to be a normal function, it actually behaves as a method of loginReplyXML
.
To send the user name and password as XML to the server and to load an XML response back into the SWF file, you can use the sendAndLoad()
method, as shown here:
// C. Send the LOGIN element to the server, // place the reply in loginReplyXML loginXML.sendAndLoad("https://www.imexstocks.com/main.cgi", loginReplyXML);
Note: This design is only an example, and Macromedia can make no claims about the level of security it provides. If you are implementing a secure password-protected system, make sure you have a good understanding of network security.
For more information, see Integrating XML and Flash in a Web Application and XML class.
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |