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 easy way

Creating an XML Packet with Flash (the easy way)

Perhaps the easiest ways to create an XML packet using Flash is to just create the XML packet directly as a string and then use the parseXML method to convert it into an XML object, as shown below:

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

  2. = function() {

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

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

  5.     login_xml.parseXML(“<LOGIN class=SpellE password='””+username_txt.text+”” username=””+password_txt.text+”” />”);

  6.     packet_txt.text = login_xml;

  7. };

  8. 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. This code is a fair bit easier to understand in contrast to the other XML example where we used createElement to build the packet. The only real pitfall with this simpler approach is that you need to be careful when typing in values that you escape the double quotes properly. In fact, the previous code could be shortened even more by building the XML packet directly in the XML constructor and therefore eliminating the need to use parseXML.

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

  2. = function() {

  3.     var login_xml:XML = new XML(“<LOGIN class=SpellE password='””+username_txt.text+”” username=””+password_txt.text+”” />”);

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

  5.     packet_txt.text = login_xml;

  6. };

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