Reputation: 467
My current code for making the XML, adding the data and saving it is:
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.events.Event;
stop();
btn_exit.addEventListener(MouseEvent.CLICK, exitConfirm); //IGNORE THIS
btn_submit.addEventListener(MouseEvent.CLICK, submitData); //Submit data event
//IGNORE THIS
function exitConfirm(e:MouseEvent):void {
gotoAndPlay(5);
}
//On "CLICK", Submit data event
function submitData(e:MouseEvent):void {
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("events.xml"));
function loadXML(e:Event):void {
xml = new XML(e.target.data);
trace(xml);
}
var file:FileReference = new FileReference;
var app:XML = <app/>;
app.titles = comp_title.text;
app.titles.category = comp_category.text;
app.titles.place = comp_place.text;
app.titles.dateDay = null;
app.titles.dateMonth = null;
app.titles.dateYear = null;
app.titles.gear = null;
file.save(app, "events.xml");
}
My problem is that it only creates this code in the XML file. I have tried adding more code onto this to first find the data on the existing XML then get that and add on the new data, but it doesn't seem to work. I'm not even sure if its possible, so that is why I have come here. Any help is good help!
P.S. AS3 is still fairly new to me. Im not horrible at it, but I aint good either :)
Cheers! :D
Upvotes: 0
Views: 126
Reputation: 12431
You need to load the existing XML and add the new data before saving the updated XML as a whole to the filesystem:
EDIT:
The following should work. I've included comments to explain certain decisions:
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.FileReference;
import flash.events.MouseEvent;
btn_exit.addEventListener(MouseEvent.CLICK, exitConfirm); //IGNORE THIS
btn_submit.addEventListener(MouseEvent.CLICK, submitData); //Submit data event
// store path in a variable so we don't need to write it out each time we need it
var xmlPath:String = "events.xml";
var xml:XML;
var loader:URLLoader;
//IGNORE THIS
function exitConfirm(e:MouseEvent):void {
gotoAndPlay(5);
}
loadXML();
// Best bet is to load the existing XML up-front because you can only trigger the
// FileReference save method from a handler invoked directly from a user interaction.
// See: http://stackoverflow.com/questions/3301839/flexs-filereference-save-can-only-be-called-in-a-user-event-handler-how-ca
function loadXML():void {
loader = new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR, xmlLoadErrorHandler);
loader.addEventListener(Event.COMPLETE, xmlLoadHandler);
loader.load(new URLRequest(xmlPath));
}
function xmlLoadHandler(e:Event):void {
xml = new XML(e.target.data);
removeListeners();
trace("Loaded XML", xml);
}
// Handle the possibility of path not existing
function xmlLoadErrorHandler(e:IOErrorEvent):void {
xml = <app></app>;
removeListeners();
trace("Error, XML did not exist, creating new empty XML");
}
// Process the data and save to file system
function submitData(e:MouseEvent):void {
var file:FileReference = new FileReference();
// Append the new title node to the existing XML.
// From now on, XML in memory matches exactly what is on file-system
xml.appendChild(getTitle());
file.save(xml, xmlPath);
}
// Returns a populated title node
function getTitle():XML {
var xml:XML = <title></title>
//replace hardcoded strings with your variables
xml.category = "category";
xml.place = "place";
xml.dateDay = "day";
xml.dateMonth = "month";
xml.dateYear = "year";
xml.gear = "gear";
return xml;
}
// It's good practice to remove event listeners when you're finished with them
function removeListeners() {
loader.removeEventListener(Event.COMPLETE, xmlLoadHandler);
loader.removeEventListener(IOErrorEvent.IO_ERROR, xmlLoadErrorHandler);
}
Upvotes: 1