修改现有XML而不影响AS3 Air应用程序的其他数据

时间:2016-08-08 18:26:14

标签: xml flash actionscript-3

我正在开发一个ActionScript 3 AIR桌面应用程序,该应用程序需要将数据保存为用户的C:\驱动器中的XML文件,以便能够在重新读取后再次保存数据。开幕。

保存过程很好,当我尝试重新打开应用程序并尝试覆盖旧值时会出现问题,除了新值之外,所有保存的数据都会被删除。

我知道错误发生的原因(基于我的代码),但我无法找到解决此问题的任何方法。我希望用户能够重新打开应用程序并检索他的旧数据,并且如果他应该覆盖现有数据条目,那么只有特定条目数据被覆盖,而不是整个数据

这是我的代码(在找到解决方案后会更新):

import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.display.Loader;
import flash.events.MouseEvent;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.utils.Timer;
import flash.events.TimerEvent;

var selfEvaluationLoader:URLLoader = new URLLoader();
var answersLoader:URLLoader = new URLLoader();
var imgLoader = new Loader();
var SEXML:XML;
var loadedXML:XML;
var SECounter = 1;
var degree:Array = new Array();
var saveDegree:Array = new Array();
var letterArr:Array = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","o")
var savedXML:XML;
var saveFlag=true;
var file:File;
var fileStream:FileStream;
var flag1:String;

nextSE.buttonMode = true;
nextSE.addEventListener(MouseEvent.CLICK,nextQuestion);
selfEvaluationLoader.load(new URLRequest("selfEval.xml"));
selfEvaluationLoader.addEventListener(Event.COMPLETE,processSEXML);
for(var b=1;b<5;b++)
    {
        SEAnswer["RB"+b].addEventListener(MouseEvent.CLICK,saveAnswer);
    }

function processSEXML(e:Event)
{
    SEXML = new XML(e.target.data);
    imgLoader.load(new URLRequest(SEXML.question.(@id==1).img.children()));
    addChild(imgLoader);
    //imgLoader.visible=false;
}

function nextQuestion(e:MouseEvent)
{
    SECounter++;
    if(SECounter == 15)
    {
        texts.alpha=1;
        nextSE.enabled=false;
        prevSE.enabled=false;
        answersLoader.load(new URLRequest("C:/Users/Safwat/My Documents/xml/saveDegree.xml"));
        answersLoader.addEventListener(Event.COMPLETE,loadDatafromC);
    }
    else
    {
        prevSE.enabled = true;
        prevSE.buttonMode = true;
        prevSE.addEventListener(MouseEvent.CLICK,prevQuestion);
        SEAnswer["hiddenRB"].selected = true;
        imgLoader.load(new URLRequest(SEXML.question.(@id==SECounter).img.children()));
        addChild(imgLoader);
    }
}
function prevQuestion(e:MouseEvent)
{
    SECounter--;
    switch(flag1)
    {
        case "RB1":
        SEAnswer["RB1"].RB1.selected = true;
        break;
        case "RB2":
        SEAnswer["RB2"].RB2.selected = true;
        break;
        case "RB3":
        SEAnswer["RB3"].RB3.selected = true;
        break;
        case "RB4":
        SEAnswer["RB4"].RB4.selected = true;
        break;
        default:
        SEAnswer["hiddenRB"].selected = true;
    }
    if(SECounter == 1)
    {
        nextSE.enabled=false;
        prevSE.enabled=false;
    }
    else
    {
        nextSE.enabled = true;
        //SEAnswer["hiddenRB"].selected = true;
        imgLoader.load(new URLRequest(SEXML.question.(@id==SECounter).img.children()));
        addChild(imgLoader);
    }
}
function saveAnswer(e:MouseEvent)
{
    degree[SECounter-1]=String(e.currentTarget.name);
    flag1=e.currentTarget.name;
    trace("flag1 is "+flag1)
    onAnswer();
}
function onAnswer()
{
    file = File.documentsDirectory.resolvePath("xml/saveDegree.xml");
    if(file.exists && saveFlag==true)
    {
    trace("here")
    answersLoader.load(new URLRequest("C:/Users/Safwat/My Documents/xml/saveDegree.xml"));
    answersLoader.addEventListener(Event.COMPLETE,retrieveSavedData);
    }
    else
    {
    saveFlag=false;
    trace("not existed")
    var fileStream:FileStream = new FileStream();
    fileStream.open(file, FileMode.WRITE);
    fileStream.writeMultiByte('<?xml version="1.0" encoding="utf-8"?>'+'\n'+'<questions>', "iso-8859-01");
    for(var kk=0;kk<14;kk++)
    fileStream.writeMultiByte('<question>'+degree[kk]+'</question>', "iso-8859-01");
    fileStream.writeMultiByte('\n'+'</questions>', "iso-8859-01");
    fileStream.close();
    }
}
function loadDatafromC(e:Event)
{
    nextSE.enabled=false;
    prevSE.enabled=false;
    loadedXML = new XML(e.target.data);
    SEAnswer.alpha=0;
    for(var b=1;b<5;b++)
    {
        SEAnswer["RB"+b].removeEventListener(MouseEvent.CLICK,saveAnswer);
    }
    for(var d=0;d<14;d++)
    {
    switch(String(loadedXML.question[d]))
    {
        case "RB1":
        texts[letterArr[d]+"1"].gotoAndStop(2);
        break;
        case "RB2":
        texts[letterArr[d]+"2"].gotoAndStop(2);
        break;
        case "RB3":
        texts[letterArr[d]+"3"].gotoAndStop(2);
        break;
        case "RB4":
        texts[letterArr[d]+"4"].gotoAndStop(2);
        break;
    }
    }
    if(SECounter==14)
    {
        texts.alpha=1;
        answersLoader.load(new URLRequest("C:/Users/Safwat/My Documents/xml/saveDegree.xml"))
        answersLoader.addEventListener(Event.COMPLETE,loadDatafromC)
    }
}
function retrieveSavedData(e:Event)
{
    savedXML = new XML(e.target.data);
    savedXML.replace(SECounter-1,<question>{degree[SECounter-1]}</question>)
    fileStream = new FileStream();

    var timer:Timer = new Timer(100, 1);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE,timedSave);
    timer.start();
}
function timedSave(e:TimerEvent)
{
    fileStream.open(file, FileMode.WRITE);
    fileStream.writeMultiByte('<?xml version="1.0" encoding="utf-8"?>'+'\n', "iso-8859-01");
    fileStream.writeMultiByte(savedXML, "iso-8859-01");
    fileStream.close();
}

0 个答案:

没有答案