Skip to main content
Participating Frequently
February 8, 2014
Answered

How to update/change the value stored in an XML record with AS3

  • February 8, 2014
  • 1 reply
  • 2060 views

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?

This topic has been closed for replies.
Correct answer kglad

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.


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;

}

1 reply

kglad
Community Expert
Community Expert
February 8, 2014

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.

funky348Author
Participating Frequently
February 8, 2014

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?

funky348Author
Participating Frequently
February 8, 2014

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.


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..