1. skip to Navigation
  2. skip to Content

Main navigation




2007 Jan 23

Flash and XML

Posted under
: work
Tagged with
: ,

Only 1 comment/pingback has been made so far – add yours?

We know that Macromedia Flash has the ability to load an xml file. And now I’m trying to share a simple way about how to load an XML file into Flash.
Firstly, let’s make an XML dummy file to be loaded later into Flash, you can use any text editor (notepad, dreamweaver…anything), write this code below :

  1. <data>
  2. <menu>elemen 1</menu>
  3. <menu>elemen 2</menu>
  4. <menu>elemen 3</menu>
  5. <menu>elemen 4</menu>
  6. <menu>elemen 5</menu>
  7. <menu>elemen 6</menu>
  8. </data>

When you have done, save it with name ‘data.xml’.

Then, let’s switch to Macromedia Flash, firstly create a dynamic text field by using ‘Type Tool’and choose ‘Dynamic Text’ on its property and give a name of your text field (here we name it ‘txtnya’), when you have done…create a new layer (just to separate between the code and everything on the main stage, so that it will be easier to make editing to the code), then open the ActionScript editor, and write these lines :

  1. my_xml = new XML() // to tell flash that we are using XML object and its methods in flash.
  2. my_xml.ignoreWhite = true; // ignore the whitespace in the XML file
  3. my_xml.onLoad = function(status){
  4. if (status){
  5. if (this.firstChild.nodeName.toUpperCase() == “DATA”) //to detect the ‘data’ tag as the first node in our XML file we just made
  6. {
  7. top = this.firstChild; //first node and its childs
  8. content=”"; //to replace the ‘undefined’ in the first node
  9. if (top != null)
  10. {
  11. menu = top.firstChild; //menu node
  12. //a loop to detect if there are ‘menu’ nodes in our xml file then print them them to the screen
  13. while (menu != null)
  14. {
  15. if(menu.nodeName.toUpperCase()==”MENU”)
  16. {
  17. isi = menu.firstChild.nodeValue; //input the value of the ‘menu’ node into a variable named ‘isi’
  18. }
  19. content += “<font size=”10″ color=”#3366cc”>”+isi+”</font>
  20. “;
  21. txtnya.html = true; //set the dynamic text field into ‘html mode’
  22. txtnya.htmlText = content; //print the value of the ‘menu’ nodes
  23. menu = menu.nextSibling; //go to the next parent node
  24. }
  25. }
  26. }
  27. }else{
  28. txtnya.text = “Failed”;
  29. }
  30. };

Last, add this one tiny line :

  1. my_xml.load(“data.xml”); //to call the XML file

Now that we’ve done and you can preview your flash movie with the XML data is formatted by flash and presented within a movie. Please remember that in this tutorial the XML data and the flash movie file are within one directory.

Hopefully this helps, and you can develop it into the form which suits your needs.
You can download the tutorial files here.

  • Trackbacks
  1. [...] my previous post i have written about Flash and XML connectivity. Based on it, I wrote a Flash code to fetch the [...]

    personal weblog of Andy Widodo — playgroundpilot.com – Flash LastFM RSS reader August 24, 2009 — 1:00 am

Have a say *) required fields


Photostream

  • 043/365: Had to settle with 'Straight'
  • 042/365: artificial
  • baby scorpion?
  • 041/365: 2012
  • oh, bullshit!
  • 040/365

Recently listening to

  • Coldplay - Paradise
  • Sheila On 7 - Sephia
  • Röyksopp - Remind Me
  • Röyksopp - Eple
  • The Cure - Mint Car
  • The Cure - Cut Here