2011-02-25 121 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