循环遍历xml-result

时间:2011-12-07 23:53:41

标签: xml actionscript-3 flex actionscript

我正在尝试遍历httpservice的结果,它是xml。

这是我尝试这样做的方式,但这不起作用。我错过了什么?

protected function getArtistsSucces(event:ResultEvent):void
    {
        var artistList:XMLList = XMLList(event.result);
        var artistXML:XML;
        for each(artistXML in artistList.artists)
        {
            var artistName:String = artistXML.artist.name;
            var artistPic:String = artistXML.artist.image.(@size=="large");
            insertArtistUrl = "http://localhost:8888/flexapp/insert_artist.php?name=" + artistName + "&pic=" + artistPic
                              + "&userid=" + userId;
            insertArtistService.send();
        }
    }

这是调试器中的结果:

result from debugger

所有节点: all nodes

3 个答案:

答案 0 :(得分:0)

您应该首先迭代artistList.artists.artist,这将代表artist节点列表(而不是其父节点)。

答案 1 :(得分:0)

我忘了添加

resultFormat="e4x"

到我的HTTPService。添加它解决了这个问题。

答案 2 :(得分:0)

下面的代码可能会解决问题。但如果问题仍然存在,请尝试跟踪otu您获得的对象。我把一些跟踪内联。 GL

  protected function getArtistsSucces(event:ResultEvent):void
    {
        var artistList:XMLList = XMLList(event.result);
        var artistXML:XML;
        for each(artistXML in artistList.artists.artist) 
        // i m not sure about "artistList.artists.artist" here.
        //Cant check it atm, need your xml data.
        {
            //trace(artistXML); // check which object comes here.

            var artistName:String = artistXML.name;
            var artistPic:String = artistXML.image.(@size=="large");
            insertArtistUrl = "http://localhost:8888/flexapp/insert_artist.php?name=" + artistName + "&pic=" + artistPic
                              + "&userid=" + userId;
            insertArtistService.send();
        }
    }