Categories

Partners
  • Website Templates

  • Design Feeds

  • Adobe Photoshop Tutorials

  • Website Templates, WordPress Themes

  • Website Templates

  • Flash Web Sites

  • Photoshop Templates

  • Photo Contests

  • Photo Tips
  • Search


    Advanced Search


    Partner Links
  • Adobe Photoshop Tutorials

  • Free Stock Photos and Images



  • Website Templates

    Creating an XML Packet with Flash - the easy way
    Published  06/19/2006 | Macromedia Flash
       




    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. submitListener.click = 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. submitListener.click = 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);

    Article Series
    This article is part 3 of a 13 part series. Other articles in this series are shown below:
    1. Flash FAQ - Introduction
    2. Streaming FLV files in Flash MX 2004
    3. Creating an XML Packet with Flash - the easy way
    4. Creating an XML Packet with Flash - the hard way
    5. Getting more colors for your Halo
    6. Loading in Images Using the MovieClipLoader Class
    7. Parsing an XML Packet in Flash
    8. Parsing XML in Flash Using the XMLConnector
    9. Sending an XML Packet to a Server-Side Page
    10. Using LoadVars.send Method to Exchange Data with a Server-Side Script
    11. Using the Alert component
    12. Using the LoadVars Class to Load Variables
    13. Using XPath in Flash MX 2004