2013-05-22 1 views
0

Adobe Flash AS3 클래스의 데이터를 XML 파일로 저장하는 데 문제가 발생했습니다. 문제는 저장시 대괄호 (<>)를 잃어 버리는 것입니다. 파일은 다음과 같이 끝 :AS3을 XML로 저장할 때 대괄호를 사용하지 않음

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

(이것은 어떻게해야입니다) : 내 프로그램에서

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

내가 보조 클래스는 모든 정보를 포함하는 경우, 두 개의 클래스가 사용자 (이름, 나이 및 균형). main 클래스는 모든 사용자를 보유하는 배열을 포함합니다.

데이터를 저장 기능 :

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") 
} 

이상한 것은이 (내가 생각하는) 일을하는 데 사용됩니다. 몇 달 전 (cs4에서) 사용했고 효과가있었습니다. 그러나 이제는 더 이상 일하지 않습니다 (CS6에서).

Nb : 노르웨이어에서 영어로 번역했습니다. 실수로 잘못 입력 한 경우 코드에없는 것일 수 있습니다.

감사합니다.

답변

0

데이터를 삽입하는 대신 이름/나이/균형 값을 재설정합니다. 따라서 원래 값을 제거합니다. = 대신 appendChild를 사용해야합니다.

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") 
} 
+0

많은 의미가 있습니다. 감사합니다. – Kartoffel