Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Sending an XML Packet to a Server-Side Page

Sending an XML Packet to a Server-Side Page

Using the XML class ‘ send or sendAndLoad methods it is possible to send an XML packet from Flash to a remote page written in a server-side language (ColdFusion, PHP, ASP, etc.) allowing you to send and retrieve data from a database or take advantage of the power of server-side languages. Imagine you had a game built in Flash. By constructing an XML packet in Flash and sending it to a server-side script it would be possible to construct a “high score” table where you could store the 10 highest scores in a database. That isn’t to say that XML is the only solution for transferring data between a Flash movie and a server-side script, in fact, far from it. You could also achieve the same result several different ways, including the LoadVars class, consuming a WebService using the WebServiceConnector or XMLConnector component in Flash MX Professional 2004 or even Flash Remoting.

To send an XML packet to a server-side template you could use the following ActionScript code:

  1. XML.prototype.ignoreWhite = true;
  2. var packet_xml:XML = new XML(“<game><name>jen</name><score>77</score></game>”);
  3. var target_xml:XML = new XML();
  4. packet_xml.xmlDecl = “<?xml version=”1.0″ ?>”;
  5. packet_xml.sendAndLoad(“http://localhost:8500/packetfromflash.cfm”, target_xml);
  6. target_xml.onData = function(data) {
  7.     _root.myText.text = data;
  8. };

There are several new concepts packed in to one small code listing. The first line of code sets the ignoreWhite property for each XML object at once by setting the property directly in the XML class instead of having to set each XML instance separately. Next two new XML objects are created from the XML class. The first instance, packet_xml will be used to hold the XML being sent to the remote server-side template. The second XML instance, target_xml, will hold the XML packet returned from the server. The sendAndLoad method works by sending an XML packet to a server-side template and receiving an XML packet as a response, which is different from the XML class’ send method which just sends an XML packet and doesn’t expect any sort of response. Each of these methods are useful in their own way but have one significant difference. The XML class’ send method takes a target as a parameter which allows you to open a new browser window or replace the content in the current browser window, whereas the sendAndLoad method takes a target XML packet as a parameter which won’t launch a new browser window in any way.

The next dazzling new bit of XML goodness in the previous example is the xmlDecl parameter which defines which XML declaration is used for the XML document. An XML declaration is responsible for defining which version of the XML specification is being used. For our packet we set the XML declaration to XML version 1.0. Using the xmlDecl property in Flash is similar to using the following code in your HTML documents: <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”> which defines the version and type of HTML being used in the HTML template. The next line in the code calls the XML object’s sendAndLoad method to send the current XML object (packet_xml) to our server-side template (http://localhost:8500/packetfromflash.cfm) and store the returned XML packet into a new XML object instance (target_xml).

The rest of the code is the onData method which is executed when Flash receives a response from the server-side template. We are using the onData method to dump the raw XML packet returned from our ColdFusion template into a text field on the Stage with an instance name of myText.

If you wanted to use the XML class’ send method instead of sendAndLoad you could change the code to the following, which would launch the target URL in a new browser (because the example code uses a target of _blank).

  1. XML.prototype.ignoreWhite = true;
  2. var packet_xml:XML = new XML(“<game><name>jen</name><score>77</score></game>”);
  3. packet_xml.xmlDecl = “<?xml version=”1.0″ ?>”;
  4. packet_xml.send(“http://localhost:8500/packetfromflash.cfm”, “_blank”);

You can see now that the code is dramatically shorter than the previous example, although now it launches a new browser window (which may or may not appear if you have a pop-up blocker installed, such as the Google Toolbar. This can be very useful if you wanted to launch a highscore table in HTML instead of Flash or display search results in HTML. The main difference is that the target_xml variable is no longer needed and is replaced with a target (in this case _blank). Other valid entries are:

  • _blank: A new unsized browser window.
  • _parent: The parent frame in a frameset.
  • _self (default): The current browser window.
  • _top: The topmost frame in a frameset.

You can also specify a custom target if you wanted to reuse the same browser. If no target is specified, the browser will use _self which could replace the currently running Flash movie with the target URL, which may not be what you want.