2011-07-05 96 views
0

我使用此代碼加載XML IM閃存:AS3將XML解析爲多維數組?

示例XML文件:

<playlist> 
    <track> 
     <title>TestTrack 1</title> 
     <band>Band1</band> 
     <file>test1.mp3</file> 
    </track> 
    <track> 
     <title>TestTrack 2</title> 
     <band>Band2</band> 
     <file>test2.mp3</file> 
    </track> 
    <track> 
     <title>TestTrack 3</title> 
     <band>Band3</band> 
     <file>test3.mp3</file> 
    </track> 
    <track> 
     <title>TestTrack 4</title> 
     <band>Band4</band> 
     <file>test4.mp3</file> 
    </track> 
</playlist> 

AS3來源:

var xmlLoader:URLLoader = new URLLoader(); 
xmlLoader.addEventListener(Event.COMPLETE, showXML); 
xmlLoader.load(new URLRequest("playlistAS3.xml")); 

function showXML(e:Event):void { 

    XML.ignoreWhitespace = true; 
    var songs:XML = new XML(e.target.data); 
    trace(songs.track.length()); 
    var i:Number; 

    for (i=0; i < songs.track.length(); i++) { 
     trace(" Name of the song: "+ songs.track[i].title.text()); 
     trace(" Name of the Band: "+ songs.track[i].band.text()); 
     trace(" File Location of the song: "+ songs.track[i].file.text()); 
    } 
} 

這裏一切正常,但我想,以做多隻是追蹤結果。 我想將結果存儲在一個對象或數組中,並從其他函數中訪問它。

我的問題是如何存儲XML數據,如果我想從其他功能使用它?

回答

1

存儲XML數據的最佳方式是在我看來課,看看下面的例子中,我提出:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class Main extends Sprite 
    { 
     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      var playlistXml:XML = <playlist> 
            <track> 
             <title>TestTrack 1</title> 
             <band>Band1</band> 
             <file>test1.mp3</file> 
            </track> 
            <track> 
             <title>TestTrack 2</title> 
             <band>Band2</band> 
             <file>test2.mp3</file> 
            </track> 
            <track> 
             <title>TestTrack 3</title> 
             <band>Band3</band> 
             <file>test3.mp3</file> 
            </track> 
            <track> 
             <title>TestTrack 4</title> 
             <band>Band4</band> 
             <file>test4.mp3</file> 
            </track> 
           </playlist>; 

      var playlist:Playlist = new Playlist(playlistXml); 

      var track:Track = playlist.getTrackByTitle("TestTrack 1"); 
      trace(track.band); // output: Band1 


     }// end function 

    }// end class 

}// end package 

internal class Playlist 
{ 
    private var _tracks:Vector.<Track>; 

    public function Playlist(playlistXml:XML) 
    { 
     _tracks = new Vector.<Track>(); 

     for each(var track:XML in playlistXml.children()) 
     { 
      _tracks.push(new Track(track.title, track.band, track.file)); 

     }// end for each 

    }// end function 

    public function getTrackByTitle(title:String):Track 
    { 
     var track:Track; 

     for each(var t:Track in _tracks) 
     { 
      if (t.title == title) track = t; 

     }// end for each 

     if (!track) throw new Error("Could not find track with title " + "\"title\"."); 

     return track; 

    }// end function 

    public function getTrackByBand(band:String):Track 
    { 
     var track:Track; 

     for each(var t:Track in _tracks) 
     { 
      if (t.band == band) track = t; 

     }// end for each 

     if (!track) throw new Error("Could not find track with band " + "\"band\"."); 

     return track; 

    }// end function 

    public function getTrackByFile(file:String):Track 
    { 
     var track:Track; 

     for each(var t:Track in _tracks) 
     { 
      if (t.file == file) track = t; 

     }// end for each 

     if (!track) throw new Error("Could not find track with file " + "\"file\"."); 

     return track; 

    }// end function 

}// end class 

internal class Track 
{ 
    private var _title:String; 
    private var _band:String; 
    private var _file:String; 

    public function get title():String { return _title } 
    public function get band():String { return _band } 
    public function get file():String { return _file } 

    public function Track(title:String, band:String, file:String) 
    { 
     _title = title; 
     _band = band; 
     _file = file; 

    }// end function 

}// end class 

在文檔類Main還有的playlistXml對象與播放列表的XML數據(沒」不要讓代碼加載它,因爲我不想讓代碼比以前更大,好吧)。

接下來是Playlist對象。這是一個類的對象,它處理分解xml中的跟蹤節點並將它們存儲在Track對象中。此外,Playlist類還有三種公共方法,分別稱爲getChildByTitle(),getChildByBand(),getChildByFile(),該方法通過相應方法搜索指定的軌道。

這種方法的優點是它創建了一個強大的結構來存儲您的數據,並允許一個簡單的方法來檢索它。

var track:Track = playlist.getTrackByTitle("TestTrack 1");    
trace(track.band); // output: Band1 
2

從我的理解,你只是想解析XML並把內容放入一個對象。事情是這樣的:

var songs:XML; 
var songsObj:Object = new Object(); 

function showXML(e:Event):void { 
    XML.ignoreWhitespace = true; 
    songs = new XML(e.target.data); 
    var len:int = songs.track.length(); 
    for (var i:int = 0; i<len; i++) { 
     trace(" Name of the song: "+ songs.track[i].title.text()); 
     trace(" Name of the Band: "+ songs.track[i].band.text()); 
     trace(" File Location of the song: "+ songs.track[i].file.text()); 
     songsObj[i] = { 
      song:songs.track[i].title.text(), 
      band:songs.track[i].band.text(), 
      file:songs.track[i].file.text() 
     }; 
    } 
    customFunction(); 
} 

function customFunction():void { 
    trace(songs.track[0].title.text()); // traces TestTrack 1 
    trace(songsObj[0].song); // also traces TestTrack 1 
} 

我不知道如果這是你在找什麼,或者你是如何想訪問其他功能的數據......但是,你也可以只使用「歌曲「XML變量,而不是創建一個實質上覆制XML中存在的數據的對象。

+0

如何在定製的Finctions中使用「歌曲」XML。對不起,愚蠢的問題,但我不熟悉as3 :(。 – Faraona

+1

如果你的函數以外有變量「歌曲」,那麼它可以在該時間軸上的所有自定義函數中訪問。不是使用Classes,而是將時間軸放在腳本上,而不是使用「songsObj [0] .song」,它會是「songs.track [0] .title.text()」。 – Corey

+0

Plese give me示例代碼 – Faraona