AS3将XML解析为多维数组?

时间:2011-07-05 18:24:23

标签: xml flash actionscript-3 parsing

我使用此代码加载XML im Flash:

示例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数据,我的问题是如何存储XML数据?

2 个答案:

答案 0 :(得分: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中存在的数据的对象。

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