AS3 Creating multidimensional-array from xml
I want to create a nested arrays. Here is my code, after I loaded xml.
function readXML(event:Event):void
{
_data = new XML(event.target.data);
for each (var usr in _data.item)
{
allUserbase.push({name: usr.@name,state: usr.@state,
complex: usr.complex,image:usr.@image, link: usr.@link});
for (var k:int = 0; k< allUserbase.length; k++){
trace(k, allUserbase[k].complex);
for (var t:int = 0; t< allUserbase[k].length; t++){
trace(k,t, allUserbase[k][t]);
// this part when i try to built 2d array doesnt work.. :(*/
}
Here is my XML:
<content>
<item image="John.jpg" name="John" state = "New Jersey" >
<complex fid = "0"> mg749</complex>
<complex fid = "1"> ks749</complex>
<complex fid = "2"> ks678</complex>
</item>
<item image="Smith.jpg" name="Smith" state = "California">
<complex fid = "0"> we649</complex>
<complex fid = "1"> sd449</complex>
<complex开发者_如何转开发 fid = "2"> df459</complex>
<complex fid = "3"> hj569</complex>
</item>
<item image="Smith.jpg" name="Mike" state = "New York">
<complex fid = "0"> 8794</complex>
<complex fid = "1"> 4384</complex>
</item>
</content>;
Is the following along the lines of what you want?:
var contentXml:XML =
<content>
<item image="John.jpg" name="John" state = "New Jersey" >
<complex fid = "0"> mg749</complex>
<complex fid = "1"> ks749</complex>
<complex fid = "2"> ks678</complex>
</item>
<item image="Smith.jpg" name="Smith" state = "California">
<complex fid = "0"> we649</complex>
<complex fid = "1"> sd449</complex>
<complex fid = "2"> df459</complex>
<complex fid = "3"> hj569</complex>
</item>
<item image="Smith.jpg" name="Mike" state = "New York">
<complex fid = "0"> 8794</complex>
<complex fid = "1"> 4384</complex>
</item>
</content>;
var contentArray:Array = new Array();
for each(var item in contentXml.item)
{
var itemArray:Array = new Array();
itemArray.push(item.@image, item.@name, item.@state);
contentArray.push(itemArray);
for each(var complex in item.complex)
{
var complexArray:Array = new Array();
complexArray.push(complex.@fid, complex);
itemArray.push(complexArray);
}// end for each
}// end for each
trace(contentXml.item[0].@image); // outputs: John.jpg
trace(contentArray[0][0]) // outputs: John.jpg
trace(contentXml.item[0].complex[0]); // outputs: mg749
trace(contentArray[0][3][1]) // outputs: mg749
[UPDATE]
You can also use a combination of Array
objects and Dictionary
objects like the following:
var contentArray:Array = new Array();
for each(var item in contentXml.item)
{
var itemDictionary = new Dictionary();
itemDictionary["image"] = item.@image;
itemDictionary["name"] = item.@name;
itemDictionary["state"] = item.@state;
var complexArray:Array = new Array();
itemDictionary["complex"] = complexArray;
contentArray.push(itemDictionary);
for each(var complex in item.complex)
{
var complexDictionary:Dictionary = new Dictionary();
complexDictionary["fid"] = complex.@fid
complexDictionary["value"] = complex;
complexArray.push(complexDictionary);
}// end for each
}// end for each
trace(contentXml.item[0].@image); // outputs: John.jpg
trace(contentArray[0]["image"]) // outputs: John.jpg
trace(contentXml.item[0].complex[0]); // outputs: mg749
trace(contentArray[0]["complex"][0]["value"]) // outputs: mg749
{name: usr.@name,state: usr.@state,
complex: usr.complex,image:usr.@image, link: usr.@link}
is not an Array
, but an Object
declaration, its elements can be accessed by allUserbase[k].complex
or allUserbase[k].['complex']
, they don't have a numeric index.
精彩评论