使用as3加载XML文件

发布于 2024-11-19 05:51:36 字数 1845 浏览 1 评论 0原文

public class loadXML extends Sprite
{
    public var xmlFileName:String;
    public var urlL:URLLoader = new URLLoader();
    public var urlR:URLRequest;
    public var xml:XML = new XML();

    public function loadXML(xmlS:String):void
    {
        xmlFileName = xmlS;
        urlR =  new URLRequest(xmlFileName);            
        urlL.addEventListener(ProgressEvent.PROGRESS, onProgressAction);
        urlL.addEventListener(Event.COMPLETE, onLoadedAction);
        urlL.addEventListener(IOErrorEvent.IO_ERROR, errorAction);
        urlL.load(urlR);            
    }
    public function onLoadedAction(e:Event):void
    {
        xml = XML(e.target.data);
        //trace(xml);
    }       
    public function onProgressAction(e:ProgressEvent):void
    {
        //trace("loading xml");
    }
    public function errorAction(e:IOError):void
    {
        trace(e.toString());
    }
}
}

//main class

package
{
import com.loadXML;
import flash.display.Sprite;

public class xmlFileView extends Sprite
{
    public var xmlData:loadXML;
    public function xmlFileView():void
    {
        init();
    }
    private function init():void
    {
        xmlData = new loadXML("list.xml");
        var xmlF:XML = XML(xmlData);
        //trace(xmlF.video[0].path);

    }
}

}

这里我实例化了loadXML 类。但我无法访问 xml,是吗?我怎样才能做到这一点?

这是我的 XML 文件。

<?xml version="1.0" encoding="iso-8859-1"?>
<videos>
    <video>
        <path>video1.flv</path>
    </video>
    <video>
        <path>video2.flv</path>
    </video>
    <video>
        <path>video3.flv</path>
    </video>
    <video>
        <path>video4.flv</path>
    </video>
</videos>
public class loadXML extends Sprite
{
    public var xmlFileName:String;
    public var urlL:URLLoader = new URLLoader();
    public var urlR:URLRequest;
    public var xml:XML = new XML();

    public function loadXML(xmlS:String):void
    {
        xmlFileName = xmlS;
        urlR =  new URLRequest(xmlFileName);            
        urlL.addEventListener(ProgressEvent.PROGRESS, onProgressAction);
        urlL.addEventListener(Event.COMPLETE, onLoadedAction);
        urlL.addEventListener(IOErrorEvent.IO_ERROR, errorAction);
        urlL.load(urlR);            
    }
    public function onLoadedAction(e:Event):void
    {
        xml = XML(e.target.data);
        //trace(xml);
    }       
    public function onProgressAction(e:ProgressEvent):void
    {
        //trace("loading xml");
    }
    public function errorAction(e:IOError):void
    {
        trace(e.toString());
    }
}
}

//main class

package
{
import com.loadXML;
import flash.display.Sprite;

public class xmlFileView extends Sprite
{
    public var xmlData:loadXML;
    public function xmlFileView():void
    {
        init();
    }
    private function init():void
    {
        xmlData = new loadXML("list.xml");
        var xmlF:XML = XML(xmlData);
        //trace(xmlF.video[0].path);

    }
}

}

Here I instantiate the loadXML class. But I can't able to access the xml, y? and how can I achieve that?

here is my XML file.

<?xml version="1.0" encoding="iso-8859-1"?>
<videos>
    <video>
        <path>video1.flv</path>
    </video>
    <video>
        <path>video2.flv</path>
    </video>
    <video>
        <path>video3.flv</path>
    </video>
    <video>
        <path>video4.flv</path>
    </video>
</videos>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2024-11-26 05:51:36

你那里的 xml 看起来不像 xml :)
这里它应该看起来像引用 trace(xmlF.video.path[0]);

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <video>
        <path>video1.flv</path>
        <path>video2.flv</path>
        <path>video3.flv</path>
        <path>video4.flv</path>
    </video>
</data>

UPDATE

你不能这样做:

xmlData = new loadXML("list.xml");
var xmlF:XML = XML(xmlData);

因为加载某些东西需要时间,并且脚本不会等到加载完成。

如果您想实现这一目标,您需要按照以下方式进行:
在 loadXML 类编辑函数中:

public function onLoadedAction(e:Event):void
{
    xml = XML(e.target.data);
    // dispatch event when the XML has been loaded and xml value is defined.
    dispatchEvent ( new Event ( Event.COMPLETE ) )
}

在 xmlFileView 类编辑中:

private function init():void
{
    xmlData = new loadXML("list.xml");
    // listen for loading completed
    xmlData.addEventListener ( Event.COMPLETE, handleXMLLoaded );
}

private function handleXMLLoaded (e:Event):void
{
   var xmlF:XML = xmlData.xml
   trace(xmlF.video.path[0]); 
}

the xml you have there is looking not like xml :)
here how it should look like reffering the trace(xmlF.video.path[0]);

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <video>
        <path>video1.flv</path>
        <path>video2.flv</path>
        <path>video3.flv</path>
        <path>video4.flv</path>
    </video>
</data>

UPDATE

you can not do like this:

xmlData = new loadXML("list.xml");
var xmlF:XML = XML(xmlData);

because loading something takes time, and script is not waiting until the loading has been done.

If you want to achive this you need to do it following way:
in the loadXML class edit function:

public function onLoadedAction(e:Event):void
{
    xml = XML(e.target.data);
    // dispatch event when the XML has been loaded and xml value is defined.
    dispatchEvent ( new Event ( Event.COMPLETE ) )
}

in the xmlFileView class edit:

private function init():void
{
    xmlData = new loadXML("list.xml");
    // listen for loading completed
    xmlData.addEventListener ( Event.COMPLETE, handleXMLLoaded );
}

private function handleXMLLoaded (e:Event):void
{
   var xmlF:XML = xmlData.xml
   trace(xmlF.video.path[0]); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文