使用AS3删除XML信息

时间:2012-06-02 08:33:35

标签: xml actionscript-3 flash

所以目前我能够根据用户需要保存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


}

先谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

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