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());
}
}
}
//主要课程
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>
答案 0 :(得分:3)
你在那里的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>
<强>更新强>
你不能这样做: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]);
}