as3 xml操作

XML的创建、获取属性

标签的属性使用@符号来获取,子标签直接.标签名称来获取

//XML的创建
var xml:XML = new XML("What you know?Steve and the flubber blubs19892006-10-17-08:31");
var xml2:XML = What you know?Steve and the flubber blubs19892006-10-17-08:31;

//XML访问
var id:String = xml.@id;
var title:String = xml.title;

输出

output:
1 What you know?

 XML遍历

var xml3:XML = new XML("What you know?Steve and the flubber blubs19892006-10-17-08:31"+
					          "Love you for everMike20042006-10-17-08:31");
var songs:XMLList = xml3.song;
//第一种遍历方式
for each(var song:XML in songs){
	trace(song.title);
}
//第二种遍历方式
for(var i:String in songs){
	trace(i + "---" + songs[i].title);
}

输出

What you know?
Love you for ever

 XML过滤

musicList是外部的一个List控件,labelFunc是控制List输出样式的

//XML的过滤
var mySong:XMLList = xml3.song.(title == "What you know?" || artist == "Steve and the flubber blubs");

//加载外部XML文件
var urlRequest:URLRequest = new URLRequest("songs.xml");
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE,compHandler);

function compHandler(e:Event):void{
	var mySongs:XML = new XML(urlLoader.data);
	musicList.dataProvider = new DataProvider(mySongs);
	musicList.labelFunction = labelFunc;
}

function labelFunc(item:Object):String{
	return item.id + "\t" + item.title + "\t" + item.artist;
}

 

你可能感兴趣的:(flash)