AS3从xml创建多维数组

时间:2011-02-25 18:03:13

标签: xml actionscript-3 multidimensional-array 2d

我想创建一个嵌套数组。在我加载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.. :(*/
 }

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

2 个答案:

答案 0 :(得分: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(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]

您还可以使用Array个对象和Dictionary个对象的组合,如下所示:

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

答案 1 :(得分:0)

{name: usr.@name,state: usr.@state, complex: usr.complex,image:usr.@image, link: usr.@link}不是Array,而是Object声明,其元素可由allUserbase[k].complexallUserbase[k].['complex']访问,但它们没有数字索引。