将XML数据加载到SWF文件时出现问题

时间:2013-09-24 05:51:32

标签: xml actionscript-3 flash

我正在尝试将高分数据加载到SWF中。我有20个动态文本字段。首字母为10,分数为10。

这是我在SWF中的动作脚本:

var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("joust_test.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);

function fileLoaded(e:Event):void
{
   xmlData = XML(loader_ul.data);

   n1_txt.text = xmlData.name1;
   n2_txt.text = xmlData.name2;
   n3_txt.text = xmlData.name3;
   n4_txt.text = xmlData.name4;
   n5_txt.text = xmlData.name5;
   n6_txt.text = xmlData.name6;
   n7_txt.text = xmlData.name7;
   n8_txt.text = xmlData.name8;
   n9_txt.text = xmlData.name9;
   n10_txt.text = xmlData.name10;
   s1_txt.text = xmlData.score1;
   s2_txt.text = xmlData.score2;
   s3_txt.text = xmlData.score3;
   s4_txt.text = xmlData.score4;
   s5_txt.text = xmlData.score5;
   s6_txt.text = xmlData.score6;
   s7_txt.text = xmlData.score7;
   s8_txt.text = xmlData.score8;
   s9_txt.text = xmlData.score9;
   s10_txt.text = xmlData.score1;

}

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<joust>
    <first>
        <name1>BBH</name1>
        <score1>515250</score1>
    </first>
    <second>
        <name2>BBH</name2>
        <score2>250600</score2>
    </second>
    <third>
        <name3>MRM</name3>
        <score3>239050</score3>
    </third>
    <fourth>
        <name4>n/a</name4>
        <score4>229800</score4>
    </fourth>
    <fifth>
        <name5>REI</name5>
        <score5>215500</score5>
    </fifth>
    <sixth>
        <name6>CTS</name6>
        <score6>177650</score6>
    </sixth>
    <seventh>
        <name7>JTK</name7>
        <score7>175150</score7>
    </seventh>
    <eighth>
        <name8>JTK</name8>
        <score8>173900</score8>
    </eighth>
    <ninth>
        <name9>ACH</name9>
        <score9>171900</score9>
    </ninth>
    <tenth>
        <name10>ZAQ</name10>
        <score10>153110</score10>
    </tenth>
</joust>

有人能帮助我弄清楚我可能做错了什么吗? SWF和XML文件位于我桌面上的同一文件夹中进行测试。

1 个答案:

答案 0 :(得分:1)

对ActionScript代码中元素的引用不考虑XML结构的层次结构。

例如,name1score1first的孩子,会被引用为:

xmlData.first.name1
xmlData.first.score1

或者,您可以使用后代访问器(..)运算符来访问XML对象的子属性:

xmlData..name1
xmlData..score1

所以,你的代码如下:

var loader:URLLoader = new URLLoader(new URLRequest("joust_test.xml"));
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);

function loaderCompleteHandler(event:Event):void
{
    var xml:XML = new XML(event.target.data);

    n1_txt.text = xml.first.name1;
    s1_txt.text = xml.first.score1;

    /* or, you can use descendant accessor */

    n2_txt.text = xml..name2;
    s2_txt.text = xml..score2;
}