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.
- <?xml version=”1.0″ encoding= “UTF-8″ ?>
- <items>
- <item item_name=”IPhone” price=”25.00″></product>
- <item item_name=”HTC” price=”10.00″></product>
- <item item_name=”Nokia” price=”50.00″></product>
- </items>
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:
// define an XML object called “xmlData”
xmlData = new XML();
// load data from an external XML file into “xmlData” object
xmlData.load(“sample.xml”);
// what to do when data is loaded … Call a function (“my_function” in this case)
xmlData.onLoad = my_function;
// ignore “white spaces”, text nodes that only contain white space are discarded
xmlData.ignoreWhite = 1;
// function contents
function my_function() {
// take the data from the XML lines (line 0,1,2) and place that data inside text fields
text_field_1.text = xmlData.firstChild.childNodes[0].attributes.item_name;
text_field_2.text = xmlData.firstChild.childNodes[1].attributes.item_name;
text_field_3.text = xmlData.firstChild.childNodes[2].attributes.item_name;
//
text_field_a.text = xmlData.firstChild.childNodes[0].attributes.price;
text_field_b.text = xmlData.firstChild.childNodes[1].attributes.price;
text_field_c.text = xmlData.firstChild.childNodes[2].attributes.price;
}
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.










pc games 10
Hello, this is a really fascinating web blog and ive loved reading several of the articles and posts contained upon the site, sustain the great work and hope to read a lot more exciting articles in the time to come.
adurodeek
it’s such a great site. fabulous, extraordinarily interesting!!!
——-
Accommodation Korcula Lumbarda
I start visiting your site and i have to say that is made with knowledge. I like it.
Issac Conch
Hi! I found your blog on Bing.It’s really comprehensive and it helped me a lot.
Continue the good work!
TOEFL ibt
Been following your blog posts for few days. Only want to leave a comment say I really love your blog. Cheers!
deal of the day
great post! thanks!
Bob Molding
Terrific Weblog. I add this Weblog to my bookmarks.