0
votes

Having difficulty grabbing data from an XML and putting it into an Array.

At the start of my document my array looks like this:

// MC's in library with exported for AS with class names "page1", "page2", etc.
var pageArray:Array = [new page1(), new page2(), new page3(), new page4(), new page5()];

Which all works well and fine when I navigate through the array.

Then my problem comes when I try to replace that array with a new array from an XML file, from within a MovieClip.

/..xml details etc../
var pages_xml:String = xmlData.account.(@code == String(codeField.text)).pages;
MovieClip(root).pageArray = pages_xml;

An example of a line from my XML would look like this.

<account code="1"><accountName>Account1</accountName><pages>new page1(), new page2()</pages></account>

So from my example above you can see that I'm just trying to replace new page1(), new page2(), new page3() with new page1(), new page2() But doing so results in this error:

TypeError: Error #1034: Type Coercion failed: cannot convert "new page1(), new page2()" to Array.

I know the best way would probably be to splice or something but the arrays will be getting messy later on and I will probably over up to 50 pages (a different selection of pages for each account code).

Would appreciate any ideas on what I can do or what I am doing wrong.

1

1 Answers

2
votes

What you're doing wrong is taking a string and assigning it to be an Array... instead it seems you want to create instances of a class based on a string as you say you'll have to parse the string then create new instances using something like

var parsedString:Array = pages_xml.split(",");
MovieClip(root).pageArray=[];
for(var i:int=0;i<parsedString.length;i++)
{
    var clazz:Class = getDefinitionByName(parsedString[i]) as Class;
    MovieClip(root).pageArray.push(new clazz());
}