XML In Flash ActionScript 3.0

XML stands for EXtensible Markup Language. XML was introduced to hierarchically structure, store and transport data or information. In ActionScript XML class was created to  handle XML Objects. But in later versions of ActionScript that is in 3.0, they implemented the powerful XML-handling standards defined in ECMAScript for XML E4X specification. This was one of the most significant changes to ActionScript 3.0.

Sample XML Document

<?xml version="1.0" encoding="utf-8" ?>
<books>
<category name="Programming">
<title>
<name>Professional JavaScript For Web Developers</name>
<author>Nicholas C. Zakas</author>
<isbn>ISBN-13: 978-0-7645-7908-0</isbn>
</title>
<title>
<name>Object-Oriented Programming with PHP5</name>
<author>Hasin Hayder</author>
<isbn>ISBN 978-1-847192-56-1</isbn>
</title>
</category>
</books>

Here, the 1st line is called XML Declaration, 2nd line <books> is the Parent Node of this sample XML. 3rd line <category> is Child Node and name is an Attribute Name and Programming is the value of that attribute. <title> is the Child Node of the <category>. This is only sample pattern of parent child relation. Hope you understood the very basic structure of the XML.

Okey, Now you just refresh the terms Parent Node, Child Node, Attributes, Attributes name, Attributes value etc in your mind. :)

Now we can quickly jump into the ActionScript coding for XML Read/Write operations. In ActionScript 3.0 the XML class contains methods and properties for working with XML objects.

Write XML inside actionscript code.

ActionScript 3.0

var books:XML = <books>
<category name="Programming">
<title>
<name>Professional JavaScript For Web Developers</name>
<author>Nicholas C. Zakas</author>
<isbn>ISBN-13: 978-0-7645-7908-0</isbn>
</title>
<title>
<name>Object-Oriented Programming with PHP5</name>
<author>Hasin Hayder</author>
<isbn>ISBN 978-1-847192-56-1</isbn>
</title>
</category>
</books>

trace(books);

Output

How to read node properties.

length property

For XML objects, this method always returns the integer 1. The length() method of the XMLList class returns a value of 1 for an XMLList object that contains only one value.

From the above example

trace("books.length() => " + books.length());
trace("books.category.length() => " + books.category.length());
trace("books.category.title.length() => " + books.category.title.length());

Output

Read Attribute value

To read the attribute information of a particular node, two methods are there.
1. Using the ‘@’ symbol to locate an attribute on a XML Object.
2. Using of attribute(“attributeName”) function.
See the Example:

trace("books.category.@['name']  => " + books.category.@['name']);
trace("books.category.@name      => " + books.category.@name);
trace('books.category.attribute("name") => ' + books.category.attribute("name"));

Output

Read the values of childNode(s)

trace("books.category.title[0].name   => " + books.category.title[0].name);
trace("books.category.title[0].author => " + books.category.title[0].author);
trace("books.category.title[1].name   => " + books.category.title[1].name);
trace("books.category.title[1].author => " + books.category.title[0].author);

These are the basic and commonly used methods when working with XML in ActionScript 3.0. Read the following posts for more details of XML and ActionScript 3.0.

Related Topics
1 . Load External XML File
2. Adding Nodes to XML Object
3. Delete a node from XML Object


Share Your Thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">