# Counting xml file elements

**URL:** <https://forum.kirupa.com/t/counting-xml-file-elements/65700>\
**Category:** flash\
**Created:** [September 28, 2004, 9:47pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700 "2004-09-28T21:47:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pneumonic](https://avatars.discourse-cdn.com/v4/letter/p/aeb1de/32.png) [@Pneumonic](https://forum.kirupa.com/u/Pneumonic)\
**Post date:** [September 28, 2004, 9:47pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700/1 "2004-09-28T21:47:09Z")

</div>

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

```php

<?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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 29, 2004, 5:33pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700/2 "2004-09-29T17:33:51Z")

</div>

Anyone? Is this impossible or what?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 29, 2004, 7:39pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700/3 "2004-09-29T19:39:36Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 29, 2004, 7:47pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700/4 "2004-09-29T19:47:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 29, 2004, 8:09pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700/5 "2004-09-29T20:09:49Z")

</div>

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

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 29, 2004, 8:33pm UTC](https://forum.kirupa.com/t/counting-xml-file-elements/65700/6 "2004-09-29T20:33:01Z")

</div>

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.
