Counting xml file elements

I’m trying to generate a random background and css file using an xml document. My xml structure is as such:


<?xml version="1.0" encoding="iso-8859-1"?>
<backgrounds> 
			  <pic>
			  	<image>http://www.21bluefish.com/images/21bluefish_logo5.jpg</image>
				<name>Logo 5</name>
				<location>My Computer</location>
			  </pic>
			   <pic>
					<image>http://www.21bluefish.com/images/Me_coat_small.jpg</image>
					<name>Me coat small</name>
					<location>Calumet City, IL</location>
			   </pic>
</backgrounds>

It’s pretty standard.

What I’ve got so far as far as code is this:


backgrounds = new Array("0.jpg", "1.jpg", "2.jpg", "3.jpg");
function randomBackground(){
	randomNumber = random (backgrounds.length);
	loadMovie (backgrounds[randomNumber], "_root.background");
}
randomBackground();
var format = new TextField.StyleSheet();
var path = "http://www.21bluefish.com/" + randomNumber + ".css";
format.load(path);
format.onLoad = function(loaded) {
	if (loaded) {
		output.styleSheet = format;
		myLoadVar = new LoadVars ();
		myLoadVar.load("john.txt")
		myLoadVar.onLoad = function (success){
			if (success == true) {
				output.variable = "john"
				output.htmlText=myLoadVar.kirupa;
				}
			}

	} else {
		output.text = "Error loading CSS file!";
	}
};

The problem is, I don’t want to use an array like I have it right now, because then updating it is a pain. What I want to do is retrieve the xml file, count the pic element and take that number and put it into a variable to multiply it by random. I then want to display the background file in an empty mc, as well as retrieve the name and location elements and display them in a text box. D**n this sounds complicated now. Well, I’m stumped so any help or a kick or push in the right position would be wonderful. Thanks.

Anyone? Is this impossible or what?

Get rid of the backgrounds element, you don’t need it.

Then use xml.childNodes.length to retrieve the number of pics.

You should now know how to do the rest. :slight_smile:

Thank you very much [m] I’ll test it out here in a second and let you know how that goes. Can I specify the node I want to retrieve or should I just divide the number by 3, since I’m using 3 seperate childNodes?

So this is what I tried…but it doesn’t work. What am I doing wrong?


function loadXML(loaded) {
	if (loaded){
		_root.image = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
		_root.name = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
		_root.location = this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;
		image_mc.loadMovie =_root.background;
		name_txt.text = _root.name;
		location_txt.text = _root.location;
	} else {
		trace("File not loaded!");
	}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("backgrounds.xml");


function randomBackground(){
	randomNumber = random (xml.childNodes.length);
	loadMovie (backgrounds[randomNumber], "_root.background");
}
randomBackground();
var format = new TextField.StyleSheet();
var path = "http://www.21bluefish.com/" + randomNumber + ".css";

Any help is appreciated. Thanks.

It is alomst a year since I’ve last worked with xml in flash. I know it has problems with retrieving the data, from like childNodes[1].nodeValue and shizzle.

So try to trace the values first (like this.firstChild.childNodes[0] , this.firstChild.childNodes[0].firstChild.nodeValue , etc just experiment) and see how to get those values out of the xml.