ActionScript2读取XML
我在“musics”文件夹中创建了一个名为“stage1.txt”的XML文件。 XML文件是:ActionScript2读取XML,xml,flash,actionscript-2,Xml,Flash,Actionscript 2,我在“musics”文件夹中创建了一个名为“stage1.txt”的XML文件。 XML文件是: <music> <speed>10</speed> <time> <note>1</note> <note>12</note> <note>32</note> <note>41</no
<music>
<speed>10</speed>
<time>
<note>1</note>
<note>12</note>
<note>32</note>
<note>41</note>
</time>
<where>
<lane>3</lane>
<lane>2</lane>
<lane>1</lane>
<lane>4</lane>
</where>
</music>
在这里之前,一切都很顺利。然而,我想回忆一下XML文件的第一个值,即“速度”。
我尝试使用以下代码:
var speed = myXML.firstChild.firstChild.nodeValue;
但它似乎不起作用。
尝试了其他方法,如:
myXML.firstChild.childNodes[0].nodeValue
但是也不起作用。您应该使用第三方库将XML转换为对象—您可以使用Greensock的—有一个简短的教程和示例代码 您的代码可能如下所示
import gs.dataTransfer.XMLParser;
XMLParser.load("musics/stage"+_global.stages+".xml", onFinish);
function onFinish($success:Boolean, $results:Object, $xml:XML):Void {
if ($success) {
trace("The speed is: "+$results.speed[0].nodeValue);1
}
}
这样,您就可以更方便(更健壮)地引用XML的节点:myMXL.speed[0].nodeValue
以您尝试的方式引用节点是非常糟糕的做法。如果XML更改,您的代码将中断,这是错误的…在其中添加一个额外的firstChild。我不太清楚为什么,但解析器似乎将文档作为一个单独的级别来处理
当事情不顺利时,找到你需要的东西的最好方法是跟踪并查看它包含的内容。因此,从xml对象('trace(myXML)')开始,然后在“trace(myXML.firstChild)->”trace(myXML.firstChild.firstChild)->等上进行遍历,直到找到所需的信息。谢谢您的帮助。我尝试在XML解析器中使用上述代码,但结果返回“速度是:[object]”。有什么想法吗?
import gs.dataTransfer.XMLParser;
XMLParser.load("musics/stage"+_global.stages+".xml", onFinish);
function onFinish($success:Boolean, $results:Object, $xml:XML):Void {
if ($success) {
trace("The speed is: "+$results.speed[0].nodeValue);1
}
}