Basic Connectivity of Flash and XML

Posted July 29th, 2010 by

In this article we will tell you about basic connection between flash and XML. It will show you what to add in the flash file in order to be able to read tags from an XML file and then use that data inside flash.

Helpful Links



Follow below mentioned steps

Step1: Make XML file

The XML file you will make look like this, you will create file called sample.xml and paste this code in that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<ol>
  <li><strong><?xml</strong> version="1.0" encoding= "UTF-8" <strong>?></strong></li>
  <li><strong><items></strong></li>
  <li>  <strong><item</strong> item_name="IPhone" price="25.00"<strong>></product></strong></li>
  <li>  <strong><item</strong> item_name="HTC" price="10.00"<strong>></product></strong></li>
  <li>  <strong><item</strong> item_name="Nokia" price="50.00"<strong>></product></strong></li>
  <li><strong></items></strong></li>
</ol>

In the above XML file the "items" are called XML tags and "item_name" and "price" are called attributes. 
"Items" is the first child of the XML file and the lines inside are child 0,1,2 of the first child.
To get the value "IPhone" you have to write this in ActionScript:
xmlData.firstChild.childNodes[0].attributes.item_name
To get the value "HTC" use this code in ActionScript:
xmlData.firstChild.childNodes[1].attributes.item_name

Step2: Make flash file

Now you have to need create 6 text fields where data item names and item price will be written but this is not important that which format will be use to display this data, in this article we just only trying to explain that how to read the XML data in flash
Now the contents in the flash file…. open Macromedia Flash, create a blank file, click on first key frame, open Actions panel and paste this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<ol>
  <li><em>// define  an XML object called "xmlData"</em></li>
  <li>xmlData = <strong>new</strong> XML();</li>
  <li><em>// load  data from an external XML file into "xmlData" object</em></li>
  <li>xmlData.load("sample.xml");</li>
  <li><em>// what to  do when data is loaded ... Call a function ("my_function" in this  case)</em></li>
  <li>xmlData.onLoad = my_function;</li>
  <li><em>// ignore  "white spaces", text nodes that only contain white space are  discarded </em></li>
  <li>xmlData.ignoreWhite = 1;</li>
  <li><em>// function  contents</em></li>
  <li><strong>function</strong> my_function() { </li>
  <li>  <em>// take the data from the XML lines (line 0,1,2) and place that  data inside text fields</em></li>
  <li>  text_field_1.text = xmlData.firstChild.childNodes[0].attributes.item_name;</li>
  <li>  text_field_2.text = xmlData.firstChild.childNodes[1].attributes.item_name;</li>
  <li>  text_field_3.text = xmlData.firstChild.childNodes[2].attributes.item_name;</li>
  <li>  <em>//</em></li>
  <li>  text_field_a.text = xmlData.firstChild.childNodes[0].attributes.price;</li>
  <li>  text_field_b.text = xmlData.firstChild.childNodes[1].attributes.price;</li>
  <li>  text_field_c.text = xmlData.firstChild.childNodes[2].attributes.price;</li>
  <li>} </li>
</ol>

As you may see, the above code loads data from an external XML file called "sample.xml" and places the data from the XML tags to text fields inside the flash file.
For example: to access the product name on first line ("IPhone") the ActionScript line inside the flash file will be like this:

xmlData.firstChild.childNodes[0].attributes.item_name

"xmlData" is the name given to the new XML object at beginning of ActionScript code; the next code are the levels, it reads from first level ("firstChild") this is "products" tag, from "products" tags it loads first child ("childNodes[0]") and the attribute name is "item_name".
So the above line will return the value "IPhone".
As you can see counting starts from zero when counting XML lines. 

Item_name, price and XML tags are defined by user, in an XML file you can name the tags and the attributes as you wish, the tags are not predefined like in HTML language.

Preview



Random Posts

Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, or follow us on Twitter. Get promotion with Hypesol.
Name: Email:
5 ResponsesLeave a comment
  • Tutorial Lounge
    July 29, 2010 at 6:38 am
    Reply

    really useful techniques you sharing. thanks

  • w3promoter
    July 30, 2010 at 11:33 am
    Reply

    Great information you are sharing.. Thanks keep up the good work.

  • cna training
    August 8, 2010 at 11:59 am
    Reply

    Keep posting stuff like this i really like it

  • WP Themes
    August 9, 2010 at 5:43 pm
    Reply

    Genial dispatch and this post helped me alot in my college assignement. Say thank you you for your information.

  • Seidu
    October 31, 2010 at 6:58 am
    Reply

    I am new to actionscript but pasting your code only gave me 1120:Access to undefined property xmlData at runtime.

Add a commentGet a Gravatar

* Name

* Email Address

Website Address

You can usethese tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  •  
  •  
  •  
Around The Site
Free Subscription

Enter your email address:

 
Become Facebook Fan
Links
Get Adobe Flash playerPlugin by wpburn.com wordpress themes