【问题标题】:Loosing brackets on saving AS3 to XML将 AS3 保存为 XML 时丢失括号
【发布时间】:2013-05-22 09:43:19
【问题描述】:

我在将数据从 Adob​​e Flash 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>

在我的程序中,我有两个类,其中第二个类包含有关每个用户的信息(姓名、年龄和余额)。主类包含一个包含所有用户的数组。

保存数据的函数:

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中)。

注:我将我的代码从挪威语翻译成英语。因此,如果我不小心拼错了某些内容,它可能不在代码中。

谢谢。

【问题讨论】:

    标签: xml actionscript-3 save appendchild brackets


    【解决方案1】:

    您正在重置姓名/年龄/余额的值,而不是在其中插入数据。因此您删除了原始值。你应该使用 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")
    }
    

    【讨论】:

    • 这很有意义!谢谢!
    猜你喜欢
    • 2012-01-16
    • 2014-05-03
    • 2022-11-25
    • 2011-09-22
    • 2012-02-03
    • 2020-11-28
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多