Saturday, September 18, 2010

Working with XML data using AS3: Part 9

Today we will learn about creating an XML Object through AS3, without loading any external documents.

If we want to create a brand new XML Object without loading any external files, we will just have to store the data we want in an XML variable.

For example:

var _data:XML = 
<contact>
<firstName>John</firstName>
<lastName>Jackson</lastName>
<age>21</age>
</contact>;

trace(_data);

This creates a simple contact nod with 3 inner tags (firstName, lastName and age). As you can see, the data is written as it is - no need for any quotation marks or any special symbols. It is automatically in the XML format. We can prove that by checking a specific tag:

trace(_data.lastName);

// This returns "Jackson"

The tag names, attributes and values can be also added dynamically. This means that we are able to store an attribute or a tag name in a variable and then retrieve it upon creating the XML object. You need to turn your variable name into { } when you're accessing it.

For example, if have a specific tag name stored in a variable ("firstName" stored in myTag), we can call myTag when creating the XML object. However, if you don't turn the variable into { }, tag will be created with the name as it is.

var myTag:String = "firstName"

var _data:XML = 
<contact>
<myTag>John</myTag>
<lastName>Jackson</lastName>
<age>21</age>
</contact>;

trace(_data);

Returns:

<contact>
  <myTag>John</myTag>
  <lastName>Jackson</lastName>
  <age>21</age>
</contact>

As you can see, the first tag in the contact nod is called myTag instead of firstName. But what happens when we turn it into { } :

var myTag:String = "firstName"

var _data:XML = 
<contact>
<{myTag}>John</{myTag}>
<lastName>Jackson</lastName>
<age>21</age>
</contact>;

trace(_data);

Output:

<contact>
  <firstName>John</firstName>
  <lastName>Jackson</lastName>
  <age>21</age>
</contact>

As you can see, the tag is now called firstName.

We can make this more complicated if we want to:

var tagArray:Array = [{tagname: "firstName", tagvalue: "John"}, {tagname: "lastName", tagvalue: "Jackson"}, {tagname: "age", tagvalue: "21"}];
var mainTag = "contact"

var _data:XML = 
<{mainTag}>
<{tagArray[0].tagname}>{tagArray[0].tagvalue}</{tagArray[0].tagname}>
<{tagArray[1].tagname}>{tagArray[1].tagvalue}</{tagArray[1].tagname}>
<{tagArray[2].tagname}>{tagArray[2].tagvalue}</{tagArray[2].tagname}>
</{mainTag}>

trace(_data);


And this returns:

<contact>
  <firstName>John</firstName>
  <lastName>Jackson</lastName>
  <age>21</age>
</contact>

Thanks for reading!

Related:

Working with XML data using AS3: Part 1
Working with XML data using AS3: Part 2
Working with XML data using AS3: Part 3
Working with XML data using AS3: Part 4
Working with XML data using AS3: Part 5
Working with XML data using AS3: Part 6
Working with XML data using AS3: Part 7
Working with XML data using AS3: Part 8
Working with XML data using AS3: Part 10

No comments:

Post a Comment