好的,这是一个准系统类,可以满足您的要求:加载 xml,解析它,一个接一个地加载图像,添加图像,添加信息文本:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class LoadImagesFromXML extends Sprite
{
private var _xmlList:XMLList;
private var _loadIndex:int;
private var _baseURL:String = "http://example.com/";
public function LoadImagesFromXML()
{
loadXML();
}
private function loadXML():void{
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener( Event.COMPLETE, onXMLComplete );
urlLoader.load( new URLRequest( _baseURL + "myxmlurl/images.xml" ) );
}
private function onXMLComplete( e:Event ):void{
var urlLoader:URLLoader = e.target as URLLoader;
urlLoader.removeEventListener( Event.COMPLETE, onXMLComplete );
var xml:XML = new XML( urlLoader.data );
loadImages( xml );
}
private function loadImages( xml:XML ):void{
_loadIndex = 0;
_xmlList = xml.Data;
loadSingleImage();
}
private function loadSingleImage():void{
var imageData:XML = _xmlList[ _loadIndex ];
var imageURL:String = imageData.Image.toString();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onImageComplete );
loader.load( new URLRequest( _baseURL + imageURL ) );
}
private function onImageComplete( e:Event ):void{
var loader:Loader = e.target.loader as Loader;
loader.y = height;
loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, onImageComplete );
addChild( loader );
var imageData:XML = _xmlList[ _loadIndex ];
var imageName:String = imageData.Name.toString();
var imagePrice:String = imageData.Price.toString();
var imageDate:String = imageData.Date.toString();
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = imageName + ", Price: " + imagePrice + ", Date: " + imageDate;
tf.y = height;
addChild( tf );
_loadIndex++;
if ( _loadIndex < _xmlList.length() ){
loadSingleImage();
}
}
}
}
xml的结构是这样的:
<?xml version="1.0" encoding="UTF-8" ?>
<images>
<Data>
<ID>127</ID>
<Name>Example 1</Name>
<Price>12!!</Price>
<Image>image1.png</Image>
<Date>Mar 08, 2013</Date>
</Data>
<Data>
<ID>127</ID>
<Name>Example 2</Name>
<Price>10!!</Price>
<Image>image2.png</Image>
<Date>Mar 20, 2013</Date>
</Data>
</images>
请注意,这段代码没有任何错误处理,但你绝对应该实现一些,以防图像不存在、url 错误或出现安全错误……</p>