;

PDA

Click to See Complete Forum and Search --> : XML question / for loop problem?


GDrider77
02-05-2009, 02:17 PM
Ok, so basically have a simple nav that needs to be populated with XML. I can trace the xml just fine, but trying to get it to poulate the textfields doesnt work.

This code works, but only for the thumb1 which is hard coded, and i dont want to have to hard code each thumb, that is why i have a for loop.
for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc.thumb1.text=bookInput.Book.author.text()[i];
trace(authorElement);

}

this is what i need, which changes thumb1 to thumb[i], but doesnt work? I know this has to be simple, just not getting it.

for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc.thumb[i].text=bookInput.Book.author.text()[i];
trace(authorElement);

}

caroach
02-05-2009, 02:49 PM
try
nav_mc["thumb"+i].text = bookInput.Book.author.text()[i];

5TonsOfFlax
02-05-2009, 02:53 PM
I hate being an enabler.

for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc["thumb"+i].text=bookInput.Book.author.text()[i]; //shouldn't this use authorElement?
trace(authorElement);
}


But really, using naming conventions is one of my pet peeves. It would be much better if your thumbs were already in an array. And it would be better still if nav_mc had a method to take the authorList and handle the changes itself.

GDrider77
02-05-2009, 03:07 PM
yeah, thanks guys, i had tried that before posting here, but no luck, i get an error. I have tried so many things, no luck. And yes 5tonsofFlax, that can be authorElement, i think i have just been trying so many different higs i never changed it back.

Get an error with your code.

Error #1010

Here is the full code.


var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("nav.xml"));


function LoadXML(e:Event):void {

xmlData=new XML(e.target.data);
ParseBooks(xmlData);

}
function ParseBooks(bookInput:XML):void {

var authorList:XMLList=bookInput.Book.author;
for (var i:int = 0; i < authorList.length(); i++) {
var authorElement:XML=authorList[i];
nav_mc["thumb"+i].text=authorElement;
trace(authorElement);
}
}

5TonsOfFlax
02-05-2009, 03:45 PM
That could be the case if you have more authors in your list than thumbs on nav_mc. Or don't have a thumb0.

GDrider77
02-05-2009, 04:20 PM
That could be the case if you have more authors in your list than thumbs on nav_mc. Or don't have a thumb0.


That was exactly the problem, no thumb0, gosh i feel stupid. Thanks guys, always helpful to have a fresh set of eyes.