Using PXP2 to build a simple XML based app in Flash
February 26th, 2007 . by polyGeekThis is a short tutorial on creating a simple XML based app in Flash. How short? Try 9 lines of Actionscript, not including comments, in the abridged version. You could then use this approach to build much more extensive apps.
Download tutorial files.
Most of the heavy lifting in this example is done with the PXP2 class: an XML-to-Object converter. The PXP2 class is included in the zip file. You don’t need to read the complete overview of PXP2 to do this tutorial.
If you’re unfamiliar with global classes then read the instructions here on how to add class files to your global path.
What we’ll build
You probably haven’t noticed but at the very bottom of every page on polyGeek.com is a randomly selected quote. It’s a very simple app that loads the quotes from an XML file and then randomly selects one of those quotes to display. Lets walk through how this works.
The 30,000 foot view
First the SWF loads a settings.xml file. That file has a list of files that contain the quotes. My thinking here was that I didn’t want to put all the quotes in one XML file just to pick one of them to display. Instead I can break the quotes up into as many files as I wish and refer to them in the settings.xml. It would also be easy enough to include data here on how to format the text, positions of the TextField, etc.
Once I have the settings.xml file loaded and parsed I can randomly select one of the quotes files to load. Then when the file with the quotes is loaded I randomly select one of the quotes and place the text in a TextField. That’s it.
The 10,000 foot view
The biggest headache in using XML files in Flash is the firstChild, nodeValue code that you have to wade through to get to your data. And then if for some reason you change the structure of the XML file then you have to edit your code. Not good.
The PXP2 class that I wrote takes care of all that for you. What you end up with is an Object that reflects the XML structure. All you really need to know about PXP2 is the signature.
With those two lines of code you have told the PXP2 class to load the XML file fileName.xml. It will then parse that XML and convert it into a usable object. When it’s all done it sends that Object to the function you specified with the second parameter - callBack. You end up in your callBack function with an Object that holds all the data from your XML. In this case it’s called “o” but you can make it anything you wish. I usually choose a variable name that reflects the rootNode of the XML file.
So if I have an XML file like such as this:
I would create an instance of PXP2() class like this:
And if I want to randomly select one of those files I would do this in the settingsCB function:
That says, take the length of the file array, multiply that by a random number, between 0 and 1, and then remove the decimal part. Since a random number is never 0 or 1 but always in between I’ll end up with a number that is between 0 and the length of the file array - 1.
The 10 foot view
The FLA is fully commented. Here’s the abridged version of the code, which is also included in the download.
The rndQuote.fla is also fully commented and deals with a few other details like adding the attribute/date, if they exist, to the quote. It also handles situations where there is only one quote file listed in the settings.xml.












Hi Dude,
Quite nice one.
Thanks
Hi,
PXP2 is fantastic, completely revolutionised the way I use flash.
However I have come across a problem and I was wondering if you could help. After following the tutorial on Outputting mySQL data as XML with PHP
from http://www.kirupa.com/web/mysql_xml_php.htm I can generate an xml file using php with a file name like “xml.php”.
It appears in the browser the same way an xml file would do but I can’t make my PXP2 load it. Can PXP2 load xml that does not have a .xml file tag like my ‘xml.php’ file?
Any help much appreciated.
Thanks
Actually I have just figured it out.
Thanks all the same - again PXP2 is really good!
Leave a Reply