Copy link to clipboard
Copied
I have the following xml file........
<Customers>
<Email address="jbloggs@me.com">
<FName>joe</FName>
<Surname>bloggs</Surname>
<DayBirth>02</DayBirth>
<MonthBirth>01</MonthBirth>
<YearBirth>1981</YearBirth>
<House>2</House>
<Address1>Johns Street</Address1>
<Address2>Trekville</Address2>
<City>Timbuktoo</City>
<PostCode>CF67TH</PostCode>
<Gender>Male</Gender>
</Email>
<Email address="fbloggs@me.com">
<FName>fred</FName>
<Surname>bloggs</Surname>
<DayBirth>03</DayBirth>
<MonthBirth>02</MonthBirth>
<YearBirth>1982</YearBirth>
<House>22</House>
<Address1>Johns Street</Address1>
<Address2>Fredville</Address2>
<City>Cardiff</City>
<PostCode>CF71TH</PostCode>
<Gender>Male</Gender>
</Email>
</Customers>
I have loaded the XMLfile into my flash website and I'm able to filter by the email address and display corresponding data. Code below....
var custXML:XMLList;
var xmlCustLoader:URLLoader = new URLLoader();
xmlCustLoader.load(new URLRequest("Customers.xml"));
xmlCustLoader.addEventListener(Event.COMPLETE, custXMLLoaded);
function custXMLLoaded(e:Event):void {
xmlCustLoader.removeEventListener(Event.COMPLETE, custXMLLoaded);
//load xml data into custXML object
custXML = new XMLList(e.target.data);
//run the parse function
ParseCustomer(custXML);
}
function ParseCustomer(customerInput:XMLList):void {
trace("XML Output");
trace("------------------------");
var custAtts:XMLList = customerInput.Email.(@address == entEmail);
for each (var custEmail:XML in custAtts) {
trace(custEmail);
}
//set variables to the filtered result
entFName = customerInput.Email.(@address == entEmail).FName;
entSurname = customerInput.Email.(@address == entEmail).Surname;
entDay = customerInput.Email.(@address == entEmail).DayBirth;
entMonth = customerInput.Email.(@address == entEmail).MonthBirth;
entYear = customerInput.Email.(@address == entEmail).YearBirth;
entHouse = customerInput.Email.(@address == entEmail).House;
entAdd1 = customerInput.Email.(@address == entEmail).Address1;
entAdd2 = customerInput.Email.(@address == entEmail).Address2;
entCity = customerInput.Email.(@address == entEmail).City;
entPCode = customerInput.Email.(@address == entEmail).PostCode;
entGender = customerInput.Email.(@address == entEmail).Gender;
}
I now want to amend a record in this file (eg. change the <FName> from fred to say Paul and save the changes back to the Customers.xml. How do i do this?
that's because you're not assigning movie name, time etc and i only showed how to solve the problem you asked originally.
you're now asking a different (but related) question. here's that solution:
var seat:int;
var chosenSeat:String;
b.addEventListener(MouseEvent.CLICK, seat1Click);
function seat1Click(event:MouseEvent):void{
seat = 1;
//set the chosenSeat to Seat1 the name of the tag in XML - this will be used to save
chosenSeat = "Seat1";
// instead of hard-coding these strings, they
...Copy link to clipboard
Copied
you can use the xml methods (like appendChild) to edit your xml and use the filereference class to save the xml (assumming it's loaded locally). if it's loaded from a server you'll need a server-side script to write to the server.
Copy link to clipboard
Copied
its stored locally in the website folder. Can you show me some example code. Everytime i try to save using file reference i have to overwrite the whole xml file. I just want to amend one item and save the xml?
Copy link to clipboard
Copied
you don't have to edit the entire xml with actionscript, but you must overwrite the entire xml file. there's no way to save part of it.
Copy link to clipboard
Copied
OK thanks for the info, i am new to actionscript and unsure how if im doing things right or efficently. How would you change and save the FName of a customer? Can you show me in code form. thanks for your help and clearing things up.
Copy link to clipboard
Copied
Another example where im trying to save a change i make is below .... (please analysis my code and tell me where im going wrong)
<cinema>
<Room name="St Davids">
<Movie title="300: Rise of an Empire">
<Day name="Tuesday">
<Showing time="14:00">
<SeatsAvail>30</SeatsAvail>
<Seat1>0</Seat1>
<Seat2>0</Seat2>
<Seat3>1</Seat3>
<Seat4>1</Seat4>
<Seat5>0</Seat5>
</Showing>
</Day>
</Movie>
</Room>
</cinema>
For record ... I have 4 Rooms,2 Movies (per room),6 Days (per movie),4 showing times (per day)
I have loaded it into my flash page ok, and have used as3 to alter the
seat data but im struggling to save the xml file back. The method i have
used is below...
yourSeats_mc.seat1_btn.addEventListener(MouseEvent.CLICK, seat1Click);
function seat1Click(event:MouseEvent):void{
yourSeats_mc.seat1_mc.visible = true;
seat1 = 1;
//set the chosenSeat to Seat1 the name of the tag in XML - this will be used to save
chosenSeat = "Seat1";
trace(seat1, " ",chosenSeat);
UpdateBooking(bookXML);
}
function UpdateBooking(movieInput:XML):void {
trace("XML Output");
trace("------------------------");
bookXML = new XML(bookLoader.data);
if (chosenSeat == "Seat1"){
//run this code and save changes to xml
trace("We are saving seat 1");
var seat1Atts:XMLList = movieInput.Room.Movie.(@title == moviePicked).Day.(@name == dayPicked).Showing.(@time == timePicked).Seat1;
for each (var seat1Free:XML in seat1Atts) {
trace(seat1Free);
var fRef:FileReference=new FileReference();
var newSeat1:XML = <Seat1>1</Seat1>;
trace(newSeat1);
var newREC:XML = movieInput.Room.Movie.(@title == moviePicked).Day.(@name == dayPicked).Showing.(@time == timePicked).replace("Seat1",newSeat1);
trace(newREC);
var addChanges1:XML = movieInput.Room.Movie.(@title == moviePicked).Day.(@name == dayPicked).replace("Showing.(@time == timePicked)",newREC);
trace(addChanges1);
var addChanges2:XML = movieInput.Room.Movie.(@title == moviePicked).replace("Day.(@name == dayPicked)",addChanges1);
trace(addChanges2);
var addChanges3:XML = movieInput.Room.replace("Movie.(@title == moviePicked)",addChanges2);
trace(addChanges3);
// var addChanges4:XML = movieInput.replace("Room",addChanges3);
// trace(addChanges4);
// var addChanges5:XML = movieInput.replace("Cinema",addChanges4);
// trace(addChanges5);
fRef.save(bookXML,"Bookings.xml");
}
//reset variable
chosenSeat = null;
}
else if (chosenSeat == "Seat2"){
//run this code and save changes to xml
//reset variable
chosenSeat = null;
}
}
NOTE:- addChanges4 and 5 are commented out because they threw up errors. My method was to overwrite the required field, then rebuild my datafile backwards... But could complete when i got to room..
Copy link to clipboard
Copied
use something like:
var seat:int;
for(var i:int=1;i<=seatNumber;i++){
yourSeats_mc["seat"+i+"_btn"].addEventListener(MouseEvent.CLICK, seatClick);
}
function seatClick(e:MouseEvent):void{
seat =int(e.currentTarget.name.split("_")[0].split("seat")[1]);
//set the chosenSeat to Seat1 the name of the tag in XML - this will be used to save
chosenSeat = e.currentTarget.name.split("_")[0];
UpdateBooking(bookXML,chosenSeat,seat);
}
function UpdateBooking(xml:XML,chosenSeat,seat):void {
xml.Room.Movie.Day.Showing[chosenSeat] = seat;
}
Copy link to clipboard
Copied
I removed my code and added yours but have issues... seatNumber undefined so i defined it as int.
Also when i run the program, the eventlistener didn't work when i click on the buttons nothing happens?
Thanks
Copy link to clipboard
Copied
you need to assign seatNumber a value equal to the number of buttons you have of the form, seat1_btn,seat2_btn,...,seatX_btn. assign,
var seatNumber:int=X;
Copy link to clipboard
Copied
When i got to this line of code:- xml.Room.Movie.Day.Showing[chosenSeat] = seat;
I got this error
TypeError: Error #1089: Assignment to lists with more than one item is not supported.
at bookingPage/UpdateBooking()[bookingPage::frame1:1241]
at bookingPage/seatClick()[bookingPage::frame1:967]
Copy link to clipboard
Copied
you're not going to assign all rooms and all movies on all days at all times are you? you should be assigning a specific room,movie,day,time.
Copy link to clipboard
Copied
all i want to do is edit the value from 0 to 1, in one specific seat, of a specific time, of a specific day, of a specific movie, of a specific room. I dont want to edit multiple items if thats what you mean? BUT i dont want to lose any data either.
The code i have used for updating is exactly the same as you have but i have the error above.
Copy link to clipboard
Copied
that's because you're not assigning movie name, time etc and i only showed how to solve the problem you asked originally.
you're now asking a different (but related) question. here's that solution:
var seat:int;
var chosenSeat:String;
b.addEventListener(MouseEvent.CLICK, seat1Click);
function seat1Click(event:MouseEvent):void{
seat = 1;
//set the chosenSeat to Seat1 the name of the tag in XML - this will be used to save
chosenSeat = "Seat1";
// instead of hard-coding these strings, they should be assigned using comboboxes or some thing else.
UpdateBooking(bookXML,chosenSeat,seat,"St Davids","300: Rise of an Empire","Tuesday","18:00");
}
function UpdateBooking(xml:XML,chosenSeat:String,seat:int,room_name:String,movie_title:String,day_name:String,showing_time:String):void {
xml.Room.(@name==room_name).Movie.(@title==movie_title).Day.(@name==day_name).Showing.(@time==showing_time)[chosenSeat] = seat;
}
Copy link to clipboard
Copied
Sorry i didnt explain it very well... But im glad to report that it worked a treat!!
Thank you so much for your help..
Just one more thing before i go ...
is it possible to automatically select "Save" on the pop up box (the window that asks for a location to save) with AS3 Code?
Copy link to clipboard
Copied
you're welcome.
and no, you have limited control when your use the filereference class. the user must interact with the save dialog.
if you use the file class (which requires an adobe air app), you have much more control over the save process.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now