尽管调用了“delete",xml 节点也不会删除. [英] xml nodes will not delete despite calling "delete"

查看:30
本文介绍了尽管调用了“delete",xml 节点也不会删除.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 delete 关键字从 xml 文件中删除节点,但它显然无法正常工作.

I'm trying to use the delete keyword to remove nodes from an xml file and it just plain won't work.

这是我正在使用的精简示例.每个节点都有一个名为deleteme"的子节点.如果它的值等于 1,我想将它从 xml 文件中删除.如果它还有什么我想离开它.delete 方法显然是 gettig 调用,但没有任何效果.

Here's a stripped down example of what I'm working with. Every node has a child named "deleteme". If its value is equal to 1 I want to remove it from the xml file. If its anything else I want to leave it be. The delete method is deffinately gettig call but it's having no effect.

<?xml version="1.0" encoding="utf-8"?>

    <stuff>
        <i>
            <deleteme>
            0
            </deleteme>
        </i>
        <i>
            <deleteme>
            1
            </deleteme>
        </i>
        <i>
            <deleteme>
            0
            </deleteme>
        </i>

    </stuff>

ActionScript

var xmlList:XMLList=_sourceXML.i;

                for (var j:int=0; j < _xmlList.length(); j++)
                {

                    if (xmlList[j].deleteme== 1)
                    {
                        delete xmlList[j];

                    }

                }
//breakpoint here. xmlList still contains nodes that should have been deleted
                xmlListColl=new XMLListCollection(xmlList);
                xmlListColl.refresh()

编辑

如果我在删除 for 循环之前和之后跟踪 xmllist 长度,则长度确实不同.似乎出于某种原因,传递给 xmllistcollection 的 xmllist 是节点未删除的那个.对我来说毫无意义.

If I trace the xmllist lenght before and after the delete for loop the length is indeed different. It seems for some reason the xmllist being passed to the xmllistcollection is the one with node not deleted. Makes no sense to me.

编辑 2

下面给出了想要的结果.但是,我仍然想知道为什么前一种方法不起作用.

The following gives the desired result. I would still however like to know why the former method was not working.

for (var j:int=0; j < xmlList.length(); j++)
                {
                    //trace(xmlList[j].deleteme)
                    if (xmlList[j].deleteme!=1 )
                    {
                        //delete xmlList[j];
                        xmlListColl.addItem(xmlList[j])

                    }

                }

推荐答案

您需要反向执行'for 循环'.这适用于通过迭代循环删除任何集合/列表中的项目.

You need to do the 'for loop' in reverse. This goes for removing items in any collection/list via an iteration loop.

当一个项目被删除时(例如当 j 为 2 时索引 2),List 中的下一个项目会填充被删除项目留下的空间(例如索引 3 变成索引 2),但是 j 增加到 3,并且移动的项目(在索引 2 中)被跳过.以下将起作用:

When an item is deleted (e.g. index 2 when j is 2), the next item in the List fills the space left by the deleted item (e.g. index 3 becmoes index 2), but j gets increased to 3, and the shifted item (in index 2) gets skipped. The following will work:

var _xmlList:XMLList=_sourceXML.i;

for (var j:int=_xmlList.length() - 1; j >= 0 ; j--)
{                   
    if (_xmlList[j].deleteme == 1)
    {
        delete _xmlList[j];                     
    }                   
}

这篇关于尽管调用了“delete",xml 节点也不会删除.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆