close

Find out more about affordable hosting sollutions:
VPS, JAVA, Web


Toppanel
Creating XML objects by passing data by reference PDF Print E-mail
Written by adobe   
Thursday, 26 February 2009 11:36
When creating an XML literal, you can also pass data by reference (from other variables) into an XML object by referencing a variable name enclosed in curly braces.

If the XML structure you are creating is not valid XML, you see a TypeError run-time error.

The following example dynamically creates an XML object based on the values provided by the user for the tag name, attribute name, attribute value, and tag contents. The example checks for the TypeError condition by surrounding the XML initialization code with a try...catch block.

Example



xmlns:mx="http://www.adobe.com/2006/mxml"
width="500" height="400"
initialize="createXML();"
>

[CDATA[
[]
public var xml:XML

private function createXML():void

{
try
{
// Create the XML object using the values provided
// by the user for the tag name, attribute name,
// attribute value and the tag's contents.
xml =
<{tagName.text}
{attributeName.text}={attributeValue.text}

>
{content.text}
{tagName.text}>;
}
catch (e:TypeError)

{
// Type error encountered while trying to create the
// XML object. The form must not be valid. Inform
// the user.
xml = <note>Fill the form to see the tag here.note>;
}

}
]]>




title="Passing XML data by reference"
layout="horizontal"
>


label="Tag name:">
id="tagName" change="createXML();"/>


label="Attribute name:">
id="attributeName" change="createXML();"/>


label="Attribute value:">
id="attributeValue" change="createXML();"/>


label="Tag content:">
id="content" change="createXML();"/>



width="100%"/>



editable="false"
width="300" height="50"
text="{xml.toXMLString()}"
/>