2011-02-25 120 views
1

我想创建一个嵌套数组。这是我的代码,加载xml后。AS3从xml创建多维数组

function readXML(event:Event):void 
{ 
_data = new XML(event.target.data); 
for each (var usr in _data.item) 
{ 
allUserbase.push({name: [email protected],state: [email protected], 
complex: usr.complex,image:[email protected], link: [email protected]}); 
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.. :(*/ 
} 

这里是我的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>; 
+0

这是您第三次在3天内询问同一个问题吗?并没有接受任何旧的答案? – Mike 2011-02-25 20:51:07

+0

哦,是的,我知道我以前见过这个问题。 – Taurayi 2011-02-25 20:55:44

+0

@Mike和@Taurayi -yes,我没有得到我的答案,并绝望,但非常感谢帮助.. – hanna 2011-02-28 14:57:07

回答

1

沿着你想要的是什么?:线

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([email protected], [email protected], [email protected]); 

    contentArray.push(itemArray); 

    for each(var complex in item.complex) 
    { 
     var complexArray:Array = new Array(); 
     complexArray.push([email protected], complex); 

     itemArray.push(complexArray); 

    }// end for each 

}// end for each 

trace(contentXml.item[0][email protected]); // 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 

[更新]

您还可以使用Array对象和Dictionary对象的组合像下面的以下内容:

var contentArray:Array = new Array(); 

for each(var item in contentXml.item) 
{ 
    var itemDictionary = new Dictionary(); 
    itemDictionary["image"] = [email protected]; 
    itemDictionary["name"] = [email protected]; 
    itemDictionary["state"] = [email protected]; 

    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"] = [email protected] 
     complexDictionary["value"] = complex; 

     complexArray.push(complexDictionary); 

    }// end for each 

}// end for each 

trace(contentXml.item[0][email protected]); // 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 
+0

您好Taurayi,非常感谢您的帮助..这是awsome ..我只是困惑关于一件事,并请原谅我,如果我问一些愚蠢的,但我如何“跟踪(contentXml.item [0] .complex [2]);//输出:ks678痕迹(contentArray [0] [3] [1 ]); //输出:mg749如何从这里访问“ks678”...再次感谢帮助.. – hanna 2011-02-28 14:54:29

+0

@ Taurayi ..我也得到了这个,谢谢很多..字典对象工作得很好..我还有一个问题,虽然我必须为此创建一个搜索函数,并且我能够正确地处理复杂的以下代码:function search(MouseEvent):void {for(var n:int = 0; n hanna 2011-02-28 17:08:14

+0

嗨..感谢您的帮助..我终于解决了这个问题..谢谢了很多.. – hanna 2011-02-28 19:12:30

0

{name: [email protected],state: [email protected], complex: usr.complex,image:[email protected], link: [email protected]}不是Array,而是一个Object声明,其内容可以访问由allUserbase[k].complexallUserbase[k].['complex'],他们没有一个数字索引。

+0

thnx回复。我如何通过创建一个2d数组访问comflex数据lilke allUserbase [0] [1] ..有没有办法做到这一点.. – hanna 2011-02-25 19:08:27