Integrating Flash with Web Applications > Sending and loading variables to and from a remote file > Using the XML object

Using the XML object

You can use the methods of the ActionScript XML object (for example, appendChild, removeNode, and insertBefore) to structure XML data in Flash to send to a server and to manipulate and interpret downloaded XML data.

You can use the following XML object methods to send and load XML data to a server via the HTTP POST method:

load downloads XML from a URL and places it in an ActionScript XML object.
send passes an XML object to a URL. Any returned information is sent to another browser window.
sendAndLoad 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 for trading securities 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 Flash movie (for example, a username 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 movie.

The flow and conversion of data between a Flash Player movie, a server-side scripting document, 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 Flash movie 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 presses 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 the script, read the commented lines of each script as indicated by the characters //:

on (release) {
	// A. Construct a 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 a 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 presses the SUBMIT button:

<LOGIN USERNAME="JeanSmith" PASSWORD="VerySecret" />

The server receives the XML, generates an XML response, and sends it back to the Flash movie. If the password is accepted, the server responds with the following:

<LOGINREPLY STATUS="OK" SESSION="rnr6f7vkj2oe14m7jkkycilb" />

This XML includes a SESSION attribute which 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 Flash movie. 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 action, and loads into the loginReplyXML object. When the data arrives, the onLoad method 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. The onLoginReply function is assigned to the frame that contains the submit button.

The onLoginReply function is defined on the first frame of the movie.
 

The onLoginReply function is defined in the first frame of the movie. To understand the script, read the commented lines of each script as indicated by the characters //:

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 plain function, it actually behaves as a method of loginReplyXML.

To send the username and password as XML to the server and to load an XML response back into the Flash movie, you can use the sendAndLoad method, as in the following:

// C. Send the LOGIN element to the server,
//    place the reply in loginReplyXML
	loginXML.sendAndLoad("https://www.imexstocks.com/main.cgi", loginReplyXML);

For more information about XML methods, see their entries in the ActionScript Dictionary.

Note: This design is only an example, and we 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.