在循环中从XMLList添加/删除

时间:2009-10-28 01:27:15

标签: xml flex loops append

我正在尝试解析我在HTTPService中通过e4x检索到的一些XML。循环起作用,对于列表中的每个剧集,它都会循环。但是,当它尝试附加到XMLList时,我收到以下错误。

TypeError: Error #1009: Cannot access a property or method of a null object reference.

我正在尝试查询本地SQLite数据库并查看该集是否存在(正在工作)以及它是否附加到一个xmllist,如果没有,则附加到另一个xmllist。

public static function seasonFavHandler(evt:ResultEvent):void {
    Application.application.ManagePage.selectedShow = 
        Application.application.ManagePage.gridFavourites.selectedItem as XML;
    episodeNumber = XML(evt.result).descendants("episode");
    var episode:Object = episodeNumber;
    for each(episode in episodeNumber) {
        currentEpisode = episode as XML;
        achkStatement = new SQLStatement();
        achkStatement.sqlConnection = dbconnection;
        achkStatement.text = "select :episodename from episodes where episodename = :episodename";
        achkStatement.parameters[":episodename"] = episode.title;
        achkStatement.addEventListener(SQLEvent.RESULT, episodeHandler);
        achkStatement.execute();
        trace(episode.title);
    }
   //Application.application.ManagePage.episodeList = episodeNumber;
   seasonHttpService.removeEventListener(ResultEvent.RESULT, seasonFavHandler);
   CursorManager.removeBusyCursor();
}

private static function episodeHandler(event:SQLEvent):void {
    var result:SQLResult = achkStatement.getResult();
    var episodeNewT:XMLList;
    var episodeWatchedT:XMLList;
    if (!result.data) {
        episodeNewT.appendChild(currentEpisode);
        //Application.application.ManagePage.gridUnwatched.addChild(currentEpisode);
    } else {
        episodeWatchedT.appendChild(currentEpisode);
        //Application.application.ManagePage.gridWatched.addChild(currentEpisode);
    }
    Application.application.ManagePage.episodeNew = episodeNewT;
    Application.application.ManagePage.episodeWatched = episodeWatchedT;
    achkStatement.removeEventListener(SQLEvent.RESULT, episodeHandler);
}

3 个答案:

答案 0 :(得分:3)

您已声明episodeNewTepisodeWatchedT,但尚未实例化它们。我还建议您使用XMLListCollection而不是XMLList,因为它在运行时更容易修改。

试试这个:

var episodeNewT:XMLListCollection = new XMLListCollection();
var episodeWatchedT:XMLListCollection = new XMLListCollection();

if (!result.data) {
    episodeNewT.addItem(currentEpisode);
} else {
    episodeWatchedT.addItem(currentEpisode);
}

Application.application.ManagePage.episodeNew = episodeNewT.copy();
Application.application.ManagePage.episodeWatched = episodeWatchedT.copy();

现在你的变量不会为null,你可以追加它们。请注意,您最后会使用XMLListCollectionXMLList转换回copy()

答案 1 :(得分:1)

这是我动态地将xml节点添加到xmllist 的解决方案。

private function addXmlChild(xmlList:XMLList, xmlNode:XML):void
{
    try
    {
        if(xmlList.children().length() != 0)
        {
            xmlList[xmlList.length() + 1] = xmlNode;
        }
        else
            xmlList[0] = xmlNode;               
        } 
        catch(error:Error) 
        {
            LogWriter.ErrorLog("addXmlChild, " + error.message);
        }
}

答案 2 :(得分:0)

但是如果你想使用XMLList,你可以尝试下一个:

var episodeNewT:XMLList;
var episodeWatchedT:XMLList;
if (!result.data) {
    episodeNewT= XMLList(currentEpisode);
    //Application.application.ManagePage.gridUnwatched.addChild(currentEpisode);
} else {
    episodeWatchedT = XMLList(currentEpisode);
    //Application.application.ManagePage.gridWatched.addChild(currentEpisode);
}