使用as3加载XML文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你那里的 xml 看起来不像 xml :)
这里它应该看起来像引用
trace(xmlF.video.path[0]);
UPDATE
你不能这样做:
因为加载某些东西需要时间,并且脚本不会等到加载完成。
如果您想实现这一目标,您需要按照以下方式进行:
在 loadXML 类编辑函数中:
在 xmlFileView 类编辑中:
the xml you have there is looking not like xml :)
here how it should look like reffering the
trace(xmlF.video.path[0]);
UPDATE
you can not do like this:
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:
in the xmlFileView class edit: