How to Convert a String to XML in ActionScript 3

Written by mark stansberry
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert a String to XML in ActionScript 3
Use AS3 XML variables to build Flash business software (Jason Reed/Photodisc/Getty Images)

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.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Adobe Flash Professional (CS3, CS4 and CS5 Versions)

Show MoreHide

Instructions

  1. 1

    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 dialogue box that appears. Click the "OK" button to close this dialogue box.

  2. 2

    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 your Name: String = new String();

  3. 3

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

    your Name = "Sally";

  4. 4

    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();

  5. 5

    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> + your Name + </name>";

  6. 6

    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);

  7. 7

    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 your Name string and the actual XML tags and text contents when Flash movie is played.

    trace(xmlConvertedString.text(), your Name, xmlConvertedString.toXMLString());

  8. 8

    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 your Name: String = new String();

    your Name = "Sally";

    var formattedStringforXML = new String();

    formattedStringforXML = "<name>" + your Name + "</name>";

    var xmlConvertedString = new XML(formattedStringforXML);

    trace(xmlConvertedString.text(), your Name, xmlConvertedString.toXMLString());

  9. 9

    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 your Name string variable and the actual tag and tag text that was placed into the XML variable.

Tips and 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(), your Name, 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(), your Name)." Note that the string variable doesn't require that you use the string text property or the parenthesis.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.