使用AS3解析XML

时间:2009-05-04 23:47:02

标签: xml flash actionscript-3 parsing e4x

这是我的整个剧本,因为我似乎无法弄清楚问题出在哪里。

症状是我添加孩子(书)的地方,也不是用拇指正确和顺序添加的适当位置。结果,令我惊讶的是,到目前为止,我能让这些出现的唯一方法是编写一个错误的跟踪语句,它会以某种方式在左上角弹出它们。任何建议将不胜感激!谢谢!!

var rowsW:Number;
var my_xW:Number;
var my_yW:Number;
var my_thumb_widthW:Number;
var my_thumb_heightW:Number;
var my_imagesW:XMLList;
var my_totalW:Number;

var container_mcW:MovieClip;
var preloaders_mcW:MovieClip;
var book:TextField = new TextField();
var author:TextField = new TextField();
var publisher:TextField = new TextField();
book.selectable = true;

var x_counterW:Number = 0;
var y_counterW:Number = 0;

var my_tweensW:Array = [];
var container_mc_tweenW:Tween;

var myXMLLoaderW:URLLoader = new URLLoader();

myXMLLoaderW.load(new URLRequest("WORKS.xml"));
myXMLLoaderW.addEventListener(Event.COMPLETE, processXMLW);

createContainerW();
callThumbsW();

function processXMLW(e:Event):void {
var myXMLW:XML = new XML(e.target.data);

rowsW = myXMLW.@ROWS;
my_xW = myXMLW.@XPOSITION;
my_yW = myXMLW.@YPOSITION;
my_thumb_widthW = myXMLW.@WIDTH;
my_thumb_heightW = myXMLW.@HEIGHT;
my_imagesW = myXMLW.IMAGE;
my_totalW = my_imagesW.length();

myXMLLoaderW.removeEventListener(Event.COMPLETE, processXMLW);
myXMLLoaderW = null;

}

function createContainerW():void {
container_mcW = new MovieClip();
container_mcW.x = my_xW;
container_mcW.y = my_yW;
addChild(container_mcW);


preloaders_mcW = new MovieClip();
preloaders_mcW.x = container_mcW.x;
preloaders_mcW.y = container_mcW.y;
addChild(preloaders_mcW);
}

function callThumbsW():void {
for (var i:Number = 0; i < my_totalW; i++) {

    var thumb_urlW = my_imagesW[i].@THUMB;
    book.text = my_imagesW[i].@TITLE;
    author.text = my_imagesW[i].@AUTHOR;
    publisher.text = my_imagesW[i].@PUBLISHER;

    var thumb_loaderW = new Loader();
    thumb_loaderW.load(new URLRequest(thumb_urlW));

thumb_loaderW.contentLoaderInfo.addEventListener(Event.COMPLETE,thumbLoadedW);

    thumb_loaderW.name = i;

    book.x = (40)*x_counterW;
    book.y = (my_thumb_heightW+40)*y_counterW;
    thumb_loaderW.x = (my_thumb_widthW+10)*x_counterW;
    thumb_loaderW.y = (my_thumb_heightW+10)*y_counterW;


    container_mcW.addChild(book);
    container_mcW.addChild(author);
    container_mcW.addChild(publisher);
    trace("my x equals" (book.x));

    if (y_counterW+1 < rowsW) {
        y_counterW++;
    } else {
        y_counterW = 0;
        x_counterW++;
    }
    var preloader_pbW:ProgressBar = new ProgressBar();
    preloader_pbW.source = thumb_loaderW.contentLoaderInfo;
    preloader_pbW.x = thumb_loaderW.x;
    preloader_pbW.y = thumb_loaderW.y;
    preloader_pbW.width = my_thumb_widthW;
    preloader_pbW.height = my_thumb_heightW;
    preloaders_mcW.addChild(preloader_pbW);

    preloader_pbW.addEventListener(Event.COMPLETE, donePbW);

}
}

function thumbLoadedW(e:Event):void {
var my_thumbW:Loader = Loader(e.target.loader);
container_mcW.addChild(my_thumbW);

my_tweensW[Number(my_thumbW.name)]=new Tween(my_thumbW, "alpha", Strong.easeIn, 0,1,0.5, true);

my_thumbW.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoadedW);
}

function donePbW(e:Event):void {
var my_pbW:ProgressBar = ProgressBar(e.target);
preloaders_mcW.removeChild(my_pbW);
my_pbW.removeEventListener(Event.COMPLETE, donePbW);
}

2 个答案:

答案 0 :(得分:1)

在我看来,您并没有正确加载XML。您应该使用URLLoader,而不是加载XML来加载XML。然后,您需要等到触发Event.COMPLETE来解析数据并添加文本字段。

来自sephiroth(http://www.sephiroth.it/tutorials/flashPHP/E4X/):

import flash.net.URLLoader
import flash.net.URLRequest
import flash.xml.XML
import flash.event.*
import flash.error.*

var mainXML:XML;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.sephiroth.it/tutorials/flashPHP/E4X/files/test.xml"));


function onComplete(evt:Event)
{
    mainXML = new XML(loader.data)
    trace("xml loaded, start parsing using E4X syntax");
}

好吧,既然你已经大量编辑了你的问题,是的,很明显你已经拥有了那个问题。在尝试将其解析为xml对象之前,我会尝试跟踪/观察loader.data,以确切了解您尝试将哪些数据解析为XML。听起来你可能得到了一些你并不期望的数据。

答案 1 :(得分:0)

我的代码最多只显示XML List中的一个结果的原因是因为我在循环之外实例化一个TextField,它从XML的许多节点中绘制var i。要正确显示每个节点的文本字段,我需要为每个var i实例化TextField。像这样:

function callThumbsW():void {
for (var i:Number = 0; i < my_totalW; i++) {

    var thumb_urlW = my_imagesW[i].thumb;
    var book = my_imagesW[i].TITLE;

    var textHolder:TextField = new TextField;
    container_mcW.addChild(textHolder);

这导致了即时的满足感。