as3 XML 值放入数组中。无法让它发挥作用
我是一个as3菜鸟,我正在努力学习。
这是我无法理解的事情。我有一个非常简单的 XML:
<?xml version="1.0" encoding="utf-8"?>
<SLIDES>
<IMAGE>01.jpg</IMAGE>
<IMAGE>02.jpg</IMAGE>
<IMAGE>03.jpg</IMAGE>
<IMAGE>04.jpg</IMAGE>
<IMAGE>05.jpg</IMAGE>
</SLIDES>
我正在像这样解析:
private function loadXML(filename:String):void {
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest(filename+".xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
myXML.ignoreWhite=true;
for (var i:int = 0; i < myXML.IMAGE.length(); i++) {
imagesURLs.push(myXML.IMAGE[i]);
trace(myXML.IMAGE[i]);
}
}
for each (var imageURL:String in imagesURLs) {
trace(imageURL);
}
}
第一个跟踪返回:
01.jpg
02.jpg
03.jpg
04.jpg
05.jpg
而第二个跟踪是空的!为什么?我真的开始失去理智了。我也尝试过:
myXML.IMAGE[i].toXMLString()
没有运气。您能帮助我并让我理解为什么会发生这种情况吗?
谢谢。
ps
该函数使用我需要解析的文件名进行调用,数组的声明如下:
public var imagesURLs:Array = [];
我正在将最新版本的 FLEX 与 flashDevelop 一起使用
I'm a noob with as3 and I'm trying to learn.
This is something that I cannot understand. I have a very simple XML:
<?xml version="1.0" encoding="utf-8"?>
<SLIDES>
<IMAGE>01.jpg</IMAGE>
<IMAGE>02.jpg</IMAGE>
<IMAGE>03.jpg</IMAGE>
<IMAGE>04.jpg</IMAGE>
<IMAGE>05.jpg</IMAGE>
</SLIDES>
That I'm parsing like this:
private function loadXML(filename:String):void {
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest(filename+".xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
myXML.ignoreWhite=true;
for (var i:int = 0; i < myXML.IMAGE.length(); i++) {
imagesURLs.push(myXML.IMAGE[i]);
trace(myXML.IMAGE[i]);
}
}
for each (var imageURL:String in imagesURLs) {
trace(imageURL);
}
}
The first trace returns:
01.jpg
02.jpg
03.jpg
04.jpg
05.jpg
While the second is empty! Why? I'm really starting to lose my mind. I also tried:
myXML.IMAGE[i].toXMLString()
With no luck. Can you please help me and make me undestand why does this happens?
Thank you.
p.s.
The function gets called with the filename I need to parse and the array is declared like so:
public var imagesURLs:Array = [];
I'm using the latest version of FLEX with flashDevelop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个循环在“processXML”函数之前执行。这时候你的Array是空的!
仅当数据可用时才会调用您的“processXML”函数(异步调用)
另外,不要忘记删除事件侦听器。
Your second loop is executed before the 'processXML' function. At this time your Array is empty !
Your 'processXML' function will be called only when data will be available (asynchronous call)
Also, don't forget to remove your event listener.