本文介绍了xml节点不会删除,尽管调用“删除”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时删除!!
我试图用delete关键字从xml文件中删除节点,它只是简单的不起作用。
下面是我正在处理的一个简单例子。每个节点都有一个名为deleteme的子节点。如果它的值等于1,我想从xml文件中删除它。如果其他任何我想离开它。
<?xml version =1.0encoding =utf -8\" >?;
< stuff>
< i>
< deleteme>
0
< / deleteme>
< / i>
< i>
< deleteme>
1
< / deleteme>
< / i>
< i>
< deleteme>
0
< / deleteme>
< / i>
< / stuff>
动作脚本 b
$ b
var xmlList:XMLList = _sourceXML.i; (xmlList [j] .deleteme = 0;
为(var j:int = 0; j< _xmlList.length(); j ++) = 1)
{
delete xmlList [j];
}
}
//此处为断点。 xmlList仍然包含应该被删除的节点
xmlListColl = new XMLListCollection(xmlList);
xmlListColl.refresh()
编辑
如果我在删除for循环之前和之后跟踪xmllist长度,则确实长度不同。似乎由于某种原因,传递给xmllistcollection的xmllist是没有被删除的节点。对我来说是没有意义的。
编辑2
结果。
for(var j:int = 0; j< ; xmlList.length(); j ++)
{
//trace(xmlList[j].deleteme)
if(xmlList [j] .deleteme!= 1)
{
//删除xmlList [j];
xmlListColl.addItem(xmlList [j])
}
}
解决方案
你需要做'for循环' 即可。这用于通过迭代循环移除任何集合/列表中的项目。
当一个项目被删除时(例如,当j是2时的索引2),列表中的下一个项目填充被删除项目留下的空间(例如index 3 becmoes index 2),但是j增加到3,并且被移位的项目(在索引2中)被跳过。以下将工作:
var _xmlList:XMLList = _sourceXML.i;
for(var j:int = _xmlList.length() - 1; j> = 0; j--)
{
if(_xmlList [j] .deleteme == 1)
{
delete _xmlList [j];
}
}
I'm trying to use the delete keyword to remove nodes from an xml file and it just plain won't work.
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()
Edit
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.
Edit 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])
}
}
解决方案
Yo
You need to do the 'for loop' in reverse. This goes for removing items in any collection/list via an iteration loop.
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];
}
}
这篇关于xml节点不会删除,尽管调用“删除”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
1403页,肝出来的..