How to Convert a String to XML in ActionScript 3

Save

Knowing how to convert strings to XML in AS3 will let you create business and office applications with Flash. AS3 XML programming skills allow you to take online customer and web visitor text inputs, store them in a database and output them to an RSS feed. But thats not all. Using AS3 to convert strings to XML will allow you to change content of a web page interactively and online. Converting a string to XML requires an AS3' XML object type. However, to successfully convert a string to XML requires that you format your string with XML tags.

Things You'll Need

  • Adobe Flash Professional (CS3, CS4 and CS5 Versions)
  • Start the Flash program. Click "New" from the File menu on the main Flash menu bar. Click the file type "Flash File(ActionScript 3.0)" from the New Document dialog box that appears. Click the "OK" button to close this dialog box.

  • Select the "Actions" option from the Window menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button and type in the code below to declare a string variable, a variable that stores text, called "yourName."

    var yourName: String = new String();

  • Type the code below starting at the next line in the Actions editor to create to store the text, "Sally" into the yourName string variable

    yourName = "Sally";

  • Type the code below in the next line of the Actions editor to declare a string variable named "formattedStringforXML" that will store XML tag formatted string.

    var formattedStringforXML = new String();

  • Type the code below in the next line of the Actions editor to concatenate the "yourName" string variable with an XML tag named "name" and store it in the "formattedStringforXML" string variable.

    formattedStringforXML = "<name> + yourName + </name>";

  • Type the code below in the next line of the Actions editor to declare an XML variable(object type) named xmlConvertedString to store the contents of the "formattedStringforXML" variable.

    var xmlConvertedString = new XML(formattedStringforXML);

  • Type the code below in the next line of the Actions editor to instruct Flash to display the text stored in the xmlConvertedString variable, the contents of the yourName string and the actual XML tags and text contents when Flash movie is played.

    trace(xmlConvertedString.text(), yourName, xmlConvertedString.toXMLString());

  • Review the code you typed in, as listed below, for syntax errors. Correct any errors as necessary. Copy and paste the code below into the Actions editor if you did not type in the code above. Use the code below to ensure that the string to XML conversion code runs correctly the first time without errors.

    var yourName: String = new String();

    yourName = "Sally";

    var formattedStringforXML = new String();

    formattedStringforXML = "<name>" + yourName + "</name>";

    var xmlConvertedString = new XML(formattedStringforXML);

    trace(xmlConvertedString.text(), yourName, xmlConvertedString.toXMLString());

  • Click the "TestMovie" option in the Control menu on the main Flash menu bar to play the movie. Observe that the output panel displays "Sally Sally <name>Sally</name>." Note this output corresponds to the text within the XML variable, the text within the yourName string variable and the actual tag and tag text that was placed into the XML variable.

Tips & Warnings

  • Concatenating in AS3 requires the use of the "+" sign operator. Text to be concatenated, such as XML tags, must always be placed in double quotation marks. Observe that the code statement "trace(xmlConvertedString.text(), yourName, xmlConvertedString.toXMLString());" uses the toXMLString method. This method takes the actual XML tags and tag contents of an XML variable and converts it to a text string so that it can be displayed.
  • Displaying the XML contents requires that you include a parenthesis pair with the XML text property attached to the XML variable as shown in the code "trace(xmlConvertedString.text(), yourName)." Note that the string variable doesn't require that you use the string text property or the parenthesis.

References

  • Photo Credit Jason Reed/Photodisc/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!