【问题标题】:Delete XML information using AS3使用 AS3 删除 XML 信息
【发布时间】:2012-06-02 08:33:35
【问题描述】:

所以目前我可以根据用户的需要保存 1-9 个 AS3 变量,它们作为文本字符串保存到 XML 文件中,但是当用户再次保存时,旧的 xml 信息仍保留在文件中,新信息被添加。 但是,我希望新信息要么覆盖旧信息,要么设置一个函数以在保存新设置之前清除/删除 xml 文件中的信息。

这是我当前的保存功能:

var soundVAR;
var conditionVAR;
var soundtoXML:XML; 
var conditiontoXML:XML; 
var test = 1;
var playingCounter = 0;
var xml:XML = <saved>
        </saved>;


//1.Button event listener
btn_Save.addEventListener(MouseEvent.CLICK, saveXML);
//2.The saveurl function which opens a URL.
function saveXML(event:MouseEvent):void {


var conditionsArray = new Array(play1Condition, play2Condition, play3Condition, play4Condition, play5Condition, play6Condition, play7Condition, play8Condition, play9Condition)

for (var i=0; i < conditionsArray.length; i++){
trace(conditionsArray[i])
if(conditionsArray[i] == true)
{
    soundVAR = soundsName[i];
    conditionVAR = conditionsArray[i];


    playingCounter++;    
} else {

    trace('no sounds are playing');
}

while (xml.item.length() < playingCounter ){
    var soundString:String =  '<sound>' + soundVAR + '</sound>';
    var conditionString:String = '<condition>' + conditionVAR + '</condition>';
    soundtoXML = new XML(soundString);
    conditiontoXML = new XML(conditionString);

    var item:XML = <item />
    item.@id = xml.item.length().toString();
    xml.appendChild(item);

    var sound:XML = xml.item[xml.item.length() - 1];
    var condition:XML = xml.item[xml.item.length() - 1];
    sound.appendChild(soundtoXML);
    condition.appendChild(conditiontoXML);
}
}
writeXML();
trace(xml);
}

function writeXML(){

var savedSounds:SharedObject = SharedObject.getLocal( 'savedSounds');
savedSounds.data.soundsxml = xml; // your variables
savedSounds.flush(); // stores data into disk


}

提前致谢。

【问题讨论】:

  • 我不知道为什么你的问题被否决了,因为负责人没有发表评论。我认为投反对票不合适,所以我投了赞成票。
  • 谢谢,我也有点困惑。

标签: xml actionscript-3 flash


【解决方案1】:

简短回答:您想在 while 循环之前调用 xml.item.setChildern('');。更长的答案:我不明白 while 循环的最后 4 行。您能解释一下编写它们的意图吗?

如果我正确理解了您的代码,那么 while 循环可能如下所示:

var i:int = xml.item.length();
while (i < playingCounter)
{
    xml.* += <item id={ i }>
          <sound>{ soundVAR }</sound>
      <condition>{ conditionVAR }</condition>
    </item>;
    i++;
}

【讨论】:

  • 首先我要感谢您的帮助。该应用程序是一个混音器,其中九个变量是不同的声音, 存储变量名称, 存储布尔值 true 或 false,具体取决于是否选择了声音。您的代码看起来不错,所以理论上这会在使用 setChildren 函数编写之前清除 xml 中的信息?
  • 我现在无法访问 Flash 文件,但我会在明天回复并告诉您是否可以正常工作,谢谢
  • 使用delete 操作符应该可以工作,就像你使用它从Object 删除属性一样 - delete xml.item.whatever;
  • 非常感谢,这也可以。我最终使用了 set Children 函数,它运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 2017-01-23
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多