AS3 从 xml 创建多维数组

发布于 2024-10-19 04:17:20 字数 1361 浏览 2 评论 0原文

我想创建一个嵌套数组。这是我加载 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>; 

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>; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2024-10-26 04:17:20

以下内容符合您的要求吗?:

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

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
江城子 2024-10-26 04:17:20

{名称:usr.@name,状态:usr.@state,
complex: usr.complex,image:usr.@image, link: usr.@link}
不是一个 Array,而是一个 Object 声明,它的元素可以通过 allUserbase[k].complexallUserbase[k].['complex'] 访问,它们没有数字索引。

{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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文