Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml-based slideshow - trouble pushing code into an array

Status
Not open for further replies.

NIA2

Technical User
Aug 30, 2006
137
AU
Hi,

I’m trying to create an xml-based slideshow in flash and am having trouble pushing some code into an array.

The following is an extract showing how my xml file is structured:

Code:
<slideshow>
<garment url="formal01.jpg">
<title>Classic Design Formal Suit</title>
<style>5993</style>
<description>Olorperat illaor sim dio od dolore velit atie modiatem adio et ipit praesectem erit, vel ipsuscip er sit velit ipisciduis nulla.</description>
<price>$???</price>
<colours>Black</colours>
<sizes>87cm to 152cm</sizes>
</garment>

<garment url="formal02.jpg">
<title>Another title goes here</title>
<style>5994</style>
<description>Olorperat illaor sim dio od dolore velit atie modiatem adio et ipit praesectem erit, vel ipsuscip er sit velit ipisciduis nulla.</description>
<price>$???</price>
<colours>Black, White</colours>
<sizes>87cm to 137cm</sizes>
</garment>

</slideshow>

The urls are stored as attributes within the garment tag and the rest of the caption is stored with separate tags nested within the garment tags:

In my .fla file I have the following code:

Code:
var ssx:XML = new XML();
ssx.ignoreWhite = true;

var currentIndex:Number = 0;
var captions:Array = new Array();
var urls:Array = new Array();

ssx.onLoad = function(success) {
    if (success) {
        var ss:Array = ssx.firstChild.childNodes; //stores pictures as separate elements
        for (i=0;i<ss.length;i++) {
            urls.push("images/" + ss[i].attributes.url);
            captions.push(ss[i].childNodes[0].firstChild.nodeValue);
        }
        frame_mc.images_mc.loadMovie(urls[currentIndex]);
        captions_txt.htmlText = captions[currentIndex];
            }
    else
    {
        trace("XML file failed to load.");
    }
}

ssx.load("formalwear.xml");

With the for loop I'm trying cycle through the ss array, pull out the url attributes as well as the rest of the caption, and assign the values to the arrays called "captions" and "urls".

Since my url was stored as an attribute, the following line of code works to push the url into the urls array:

urls.push("images/" + ss.attributes.url);

...however, the captions line entered as follows doesn’t push the captions into the captions array:

captions.push(ss.childNodes[0].firstChild.nodeValue);

It only outputs the information contained in the <title> tag, ie. Classic Design Formal Suit

Since the caption isn't stored as an attribute, I assume I need the following code to be pushed into the captions array, but I don’t know how to do it.

Code:
        var caption:String = ""
        caption += "<b>"
        caption += ss[0].childNodes[0].firstChild.nodeValue;
        caption += "\n\n</b>";
        caption += "<b>Style Number</b>\n"
        caption += ss[0].childNodes[1].firstChild.nodeValue;
        caption += "\n\n";
        caption += "<b>Description</b>\n"
        caption += ss[0].childNodes[2].firstChild.nodeValue;
        caption += "\n\n";
        caption += "<b>Price</b>\n"
        caption += ss[0].childNodes[3].firstChild.nodeValue;
        caption += "\n\n";
        caption += "<b>Colours</b>\n"
        caption += ss[0].childNodes[4].firstChild.nodeValue;
        caption += "\n\n";
        caption += "<b>Sizes</b>\n"
        caption += ss[0].childNodes[5].firstChild.nodeValue;

Can someone tell me how I can fix the code?

Appreciate any help offered.
 
You need to use for() loop within the for() loop to go through all the child nodes. I would use Object to store the data (rather than storing in separate Arrays like you're doing) as it is much neater:
Code:
var ssx:XML = new XML();
ssx.ignoreWhite = true;

var dataArray:Array = new Array();

ssx.onLoad = function(success):Void  {
	if (success) {
		var ss:Array = ssx.firstChild.childNodes;//stores pictures as separate elements
		for (var i:Number = 0; i<ss.length; i++) {
			var garment:XMLNode = ss[i];
			var garmentObject:Object = new Object();
			garmentObject["url"] = garment.attributes.url;
			for (var j:Number = 0, k:Number = garment.childNodes.length; j<k; j++) {
				var element:XMLNode = garment.childNodes[j];
				garmentObject[element.nodeName] = element.firstChild.nodeValue;
			}
			dataArray.push(garmentObject);
		}
		doSomething();
	} else {
		trace("XML file failed to load.");
	}
};

ssx.load("formalwear.xml");

function doSomething():Void {
	for (var i:Number = 0, j:Number = dataArray.length; i<j; i++) {
		trace("Garment "+i);
		var garment:Object = dataArray[i];
		for (var s in garment) {
			trace(s+": "+garment[s]);
		}
		trace("");
	}
}
Output:

Garment 0
sizes: 87cm to 152cm
colours: Black
price: $???
description: Olorperat illaor sim dio od dolore velit atie modiatem adio et ipit praesectem erit, vel ipsuscip er sit velit ipisciduis nulla.
style: 5993
title: Classic Design Formal Suit
url: formal01.jpg

Garment 1
sizes: 87cm to 137cm
colours: Black, White
price: $???
description: Olorperat illaor sim dio od dolore velit atie modiatem adio et ipit praesectem erit, vel ipsuscip er sit velit ipisciduis nulla.
style: 5994
title: Another title goes here
url: formal02.jpg

Kenneth Kawamoto
 
Thanks for the code - I'll try it out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top