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
  • 2061 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?

kglad
Community Expert
Community Expert
February 8, 2014

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.