The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
This tutorial will teach you how to load XML data into Flash, and display a basic result.
View an Example of this article before you get started.
If you want to skip the overall setup here is the completed movie. Just click on frame 1 for the code.
Here is the code.
importXML = new XML();
importXML.ignoreWhite = true;
importXML.onLoad = function(success) {
if(success) {
var depth = 0;
var nextY = 10;
count = 0;
var root = this.firstChild; // The root node
var attr = root.attributes;
for(var i = root.firstChild; i != null; i = i.nextSibling) {
tempMC = attachMovie ("box", "box" + count, depth ++,
{_y : (nextY) , _x : (105)} );
nextY += tempMC._height + 2;
tempMC.name_txt.text = i.attributes.name;
count ++;
}
}
}
importXML.load("importdata.xml");
That is the actionscript we will use, now lets set up the movie.
Create a new flash movie, Create a new movie clip (Insert > New Symbol) draw a rectangle on the stage.
Make a new layer, name it 'txt', and add a dynamic textbox to the stage. set the instance name to 'name_txt'
Open the library, right click on the clip and check the Export option for Actionscript.
Now you are ready to start coding, click on frame 1, and either type the above code or copy & paste it into the code window. Ok lets start explaining the code.
importXML = new XML(); importXML.ignoreWhite = true;
The first line is declaring a new class. The name we use is "importXML". The next line is telling the XML parser to ignore any whitespace in the XML file.
importXML.onLoad = function(success) {
this is a function that will load the xml file, with a result of success True/False.
if(success) {
What is being done here is confirming the xml was loaded and if it was continue with the code.
var depth = 0; var nextY = 10; count = 0; var root = this.firstChild; // The root node var attr = root.attributes;
Now we will define a few variables to use later on. 'root' is the first piece of the xml file. In this example 'root' = select. 'attr' is the attributes found after the first node or parent node.
for(var i = root.firstChild; i != null; i = i.nextSibling) {
tempMC = attachMovie ("box", "box" + count, depth ++,
{_y : (nextY) , _x : (105)} );
nextY += tempMC._height + 2;
tempMC.name_txt.text = i.attributes.name;
count ++;
}
for loop that will run through all the nodes in the xml file. "i != null" is saying keep running this loop until there are no more nodes. "i = i.nextSibling" is saying to read the next node in the xml. The attachMovie is taking X,Y parameters. The difference is the Y is set by the height of the previous clip. The next line is to display the text in the movie clip. This sends the "name" attribute from the xml into the "name_txt" textbox we created earlier. Final part in the for loop is to increment the count variable.
}
}
importXML.load("importdata.xml");
This is the part where Actionscript actually loads the xml file, and calls the onLoad function.
test the results. "Ctrl + Enter" If it didn't work you can post a question in the feedback form or you can contact me.
That is the end this weeks tutorial. I would love to see what amazing things you use this for.
|
Christopher Smith Thu Jun 1, 2006 2:33 am
Here's something that some might find useful. Maxomatic at proto.layer51.com made an wonderful class for converting XML into an Array of Objects easily. There is an AS 2.0 version of this somewhere but I forget where. The AS 1.0 version is here: http://proto.layer51.com/d.aspx?f=1311
|
|
Matt Wed Oct 18, 2006 7:05 am
Hi folks! well done on your XML coding!
I got question for you? In addition to the above code, I was wondering you can pull xml info from a list that includes 3 objects: image/ name/ bio What I am trying to do is make a list of Music Artists pop up in Swishmax (using dynamic text) and when the user clicks on any name, the xml info is picked up and displays the selection. is this possible guys? my email is: matthewbalzan@hotmail.com much appreciated Matt :-) |
|
hai to all Thu Feb 8, 2007 7:48 am
thanks for toturial
i had loaded the contents from the xml. but before loading i am getting the error in text box like _level10.desc_text... like that and displying the contents in text box. i want that _level10.desc_text... should not be displayed. i know it is taking time to load and to display but until that how to hide the text box? |
|
flyingace Tue Mar 13, 2007 12:59 pm
I believe that the AS 2 version of the XML-Array class mentioned above can be found here:
http://proto.layer51.com/d.aspx?f=1340#c2521 |
|
kefsan Sun Jul 8, 2007 7:01 pm
i am iranian.
content of site is very good. thank you. good luck. |
|
dhouse Fri Jul 27, 2007 8:18 am
I don't know what is happening with my script. I think my xml may be written wrong. But when I run this code and do a trace on i at the begininng the out put is the entire xml page as one long string.
Any clue? Thanks |
|
sheetal bhatia Tue Jul 31, 2007 6:43 am
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.
please help i am very much confused. Regards, Sheetal Bhatia |
|
sheetal bhatia Tue Jul 31, 2007 6:44 am
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.
please help i am very much confused. Regards, Sheetal Bhatia |
|
sheetal bhatia Tue Jul 31, 2007 6:44 am
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.
please help i am very much confused. Regards, Sheetal Bhatia |
|
sheetal bhatia Tue Jul 31, 2007 6:45 am
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.
please help i am very much confused. Regards, Sheetal Bhatia |
|
Ranjith Tue Sep 30, 2008 5:31 am
Hi Friends,
Can you please guide how to load the XML into an Flash CS3 accordian. Thanks in Advance Ranjith Kumar |
|
Village idot Fri Oct 17, 2008 7:35 pm
I am having head banging trouble with calling my xml playlist to my JW player. I keep getting an Error stating that the xml file is not recognized...something or another. What steps should I take.
I do not know if it is my server configurations, or the file directory, or the file itself |
|
mkeefe Fri Oct 17, 2008 11:31 pm
@Village idot - What error (in Flash) are you getting? That will help me debug the problem for you.
|
|
Banarjee Wed Dec 3, 2008 6:59 am
hi
I just copy and past the code which is there above i need to hide the dynamic text field when the other menu is clicked in the same stage can u guide me in this. Regards Banarjee.Rajan |
|
mkeefe Wed Dec 3, 2008 10:24 am
@Banarjee - If you need to hide a movieclip you would simply set the
visible property to false or the _alpha property to 0 (zero).Hope that helps. |
|
vp Thu Jan 22, 2009 7:17 am
sfg
|
|
Carlos Lages Wed Oct 28, 2009 9:43 pm
You saved my job.
Thanks a lot! |
|
shwet Mon Dec 14, 2009 11:44 am
Hi,
Thanks, this work fine but when i am using this on a document width 996,it's not proper positioning.Pls help me to position it to top,left. Thanking you shwet |
|
Daren Wed Dec 16, 2009 12:54 am
Hello,
Nice content. Keep adding more things into it. Good luck |
|
Download Tue Feb 16, 2010 3:52 pm
You saved my job.
|
|
lex frost Fri Mar 12, 2010 12:29 pm
This works beautifully for me when previewing locally but as soon as i upload it doesn't load my xml feed. Any ideas anyone?
Many thanks Lex |
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN