Question

I have encountered a problem with saving data from an Adobe Flash AS3 class to a XML file. The problem is that I'm loosing some of the brackets(<>) on saving. The file ends up like this:

<UserData>
  <User>
    John
    18
    100
  </User>
  <User>
    Olav
    16
    10
  </User>
</UseData>

Instead of this (this is how it should be):

<UserData>
  <User>
    <Name>John</Name>
    <Age>18</Age>
    <Balance>100</Balance>
  </User>
  <User>
    <Name>Olav</Name>
    <Age>16</Age>
    <Balance>10</Balance>
  </User>
</UserData>

In my program I have two classes, where the secondary class contains information about every user (name, age and balance). The main class contains an array that holds all the users.

The function that saves the data:

function saveData(e:Event):void
{
    var fileRef:FileReference=new FileReference() 
    var userData:XML=new XML(<UserData/>)
    for(var i:int=0;i<userArr.length;i++)  //userArr is the array holding all the users
    {
        var user:XML=new XML(<User/>)
        var name:XML=new XML(<Name/>)
        var age:XML=new XML(<Age/>)
        var balance:XML=new XML(<Balance/>)
        name=XML(userArr[i].name) //the userArr holds a class with the variables name, age and balance.
        age=XML(userArr[i].age)
        balance=XML(userArr[i].balance)

        //I'm pretty sure that its here it goes wrong.
        //fore some reason when I appendChild the user, it gets <> but when I
        //appendChild name, age and balance it does not get a <>.
        user.appendChild(name)
        user.appendChild(age)
        user.appendChild(balance)

        userData.appendChild(user)

    }
    fileRef.save(userData,"Data.xml")
}

The odd thing is that this used to work (I think). I used it some months ago (in cs4), and it worked. But now it is no longer working (in cs6).

Nb: I translated my code from Norwegian to English. So if I accidentally misspelled something it is probably not in the code.

Thank you.

Was it helpful?

Solution

You are reseting the value of name/age/balance instead of inserting the data within. thus you remove the original value. You should have used appendChild instead of =.

function saveData(e:Event):void
{
    var fileRef:FileReference=new FileReference() 
    var userData:XML=new XML(<UserData/>)
    for(var i:int=0;i<userArr.length;i++)  //userArr is the array holding all the users
    {
        var user:XML=new XML(<User/>)
        var name:XML=new XML(<Name/>)
        var age:XML=new XML(<Age/>)
        var balance:XML=new XML(<Balance/>)
        name.appendChild(userArr[i].name) //the userArr holds a class with the variables name, age and balance.
        age.appendChild(userArr[i].age)
        balance.appendChild(userArr[i].balance)

        //I'm pretty sure that its here it goes wrong.
        //fore some reason when I appendChild the user, it gets <> but when I
        //appendChild name, age and balance it does not get a <>.
        user.appendChild(name)
        user.appendChild(age)
        user.appendChild(balance)

        userData.appendChild(user)

    }
    fileRef.save(userData,"Data.xml")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top