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!

Creating an XML Packet with Flash – the hard way

Creating an XML Packet with Flash (the hard way)

One way of building XML packets from scratch using ActionScript is to use the createElement method to make the new XML node and then populate the packet by directly setting attributes and values, as shown below:

  1. var submitListener:Object = new Object();

  2. submitListener.click = function() {

  3.     var login_xml:XML = new XML();

  4.     login_xml.xmlDecl = “<?xml version=”1.0″ ?>”;

  5.     var loginElement:XMLNode = login_xml.createElement(“login”);

  6.     loginElement.attributes.username = username_txt.text;

  7.     loginElement.attributes.password = password_txt.text;

  8.     login_xml.appendChild(loginElement);

  9.     packet_txt.text = login_xml;

  10. };

  11. submit_btn.addEventListener(“click”, submitListener);

 

But before you can test this code you need to add a few components to your Stage. First, drag two TextInput components on to your Stage and give them the instance names “username_txt” and “password_txt”. Next we need to add a Button component with the instance name submit_btn. Finally add a TextArea component and assign it an instance name of packet_txt. You may want to resize the components and add any appropriate labels or static text to the Flash document to make it a bit more user friendly, you can use the SWF file below as a general guide.

Once you’ve added the required components and given them the proper instance names, you are free to test the movie. Enter a value in both the username and password field and click the submit button. The Flash document will create a new XML packet on the fly, append the values of username_txt and password_txt, set the appropriate XML declaration and display the final packet in the giant text area.

Comments