Wednesday, April 25, 2007

Developing a flash piece to read an XML doc and then randomly show images.

We have an XML document that has names of people and the associated image for that person. The structure is

people
+ person
++ name
++ picture
+ person
+ person
++ name
++ picture
+ person
people


We'll load the document into flash.
Then we'll loop through each person node and extract the name and picture values.
We push those values into a two dimensional array.
After we have inserted all the values into the array we'll get the length of the array.
We'll take that value and pass it into a random number generator.
We'll generate a number no greater that the array length minus one. (if there five people we need a number between zero and four since arrays start numbering at zero and if we returned five we would get an error.)
Then we'll take that number and use it to get a person from the array.

So if we have five people in the XML document we will generate a number ranging from zero through four
If we return the number three we get the fourth person in the XML document.

So we Just edit the XML document to add or remove people and never have to touch the flash source code to make changes.