as 3读取xml实现代码
as 3读取xml实现代码
as3对xml的操作实在是太方便了(e4x ecmascript for xml 没错.就是ecmascript 如果js比较好的同学对这个也很熟悉吧)..
package
{
import flash.display.*;
import flash.text.*;
import flash.xml.*;
import flash.net.*;
import flash.events.event;
public class classsubfeed extends sprite
{
var temp:textfield;
var myxmlurl:urlrequest;
var myloader:urlloader;
var xmlurl:string;
var xmlcontent:xml;
public function classsubfeed()
{
temp = new textfield();
temp.x = 10;
temp.y = 10;
temp.width = 530;
temp.height = 350;
temp.border = true;
temp.bordercolor = 0x669900;
temp.multiline = true;
temp.htmltext = 'loading.....';
addchild(temp);
// xml地址
xmlurl = "http://www.226511.cn/xml/5230.xml";
this.loadxml(xmlurl);
}
private function loadxml(xmlurl:string):void
{
myxmlurl = new urlrequest(xmlurl);
myloader = new urlloader(myxmlurl);
myloader.addeventlistener(event.complete, onloaded);
}
private function onloaded(event):void
{
xmlcontent = xml(myloader.data);
temp.htmltext = '已经读取feed,正在准备.....';
this.parsedata(xmlcontent);
}
private function parsedata(xmlcontent:xml):void
{
var rrstitle:string = xmlcontent.channel.title;
var rrslink:string = xmlcontent.channel.link;
temp.htmltext = "<h3><a href='" + rrslink + "' target='_blank'>" + rrstitle + "</a></h3><br /><br />";
for each(var item:xml in xmlcontent..item)
{
var itemtitle:string = item.title.tostring();
var itemlink:string = item.link.tostring();
temp.htmltext += "<div><h4><a href='" + itemlink + "' target='_blank'>" + itemtitle + "</a></h4></div><br /><br />";
}
}
}
}
实例二,读取外部xml 文件
<?xml version="1.0" encoding="utf-8"?>
<mdragon table="xxxxx" question1="xxxxxx" question2="xxxxxxxxxxxxxxx" time="1" background="2"><question answer="a" value="期间,与人说话时)">
<answers value="a:戴口罩"/>
<answers value="b:保持一段距离"/>
<answers value="c:与往常一样"/>
<answers value="d:少说几句"/>
</question>
</mdragon>
------------------------------readxml.as-------------------------------------------package
{
import flash.events.event;
import flash.net.urlloader;
import flash.net.urlrequest;
import flash.xml.xmldocument;
import flash.text.textfield;
import flash.text.textformat;
import flash.text.textfieldautosize;
import flash.display.displayobjectcontainer;
import flash.display.spritepublic class readxml extends sprite
{
private var myxml:xml;
private var myloader:urlloader;
private var string:string;
private var mystr:string=new string();
private var myxmlurl:urlrequest;
private var txt:textfield=new textfield();
private var format:textformat = new textformat();
public function readxml(myxmlurl:string):void
{
myxmlurl=new urlrequest(myxmlurl);
myloader=new urlloader();
myloader.addeventlistener(event.complete,xmlloaded);
myloader.load(myxmlurl);
}
public function xmlloaded(evt:event):void
{
myxml=new xml(evt.currenttarget.data);
mystr=this.myxml.question[0].@value;
txt.text=mystr;
txt.autosize = textfieldautosize.left;
txt.background = true;
txt.border = true;
format.font = "宋体";
format.color = 0x000000;
format.size = 15;
format.underline = false;txt.defaulttextformat=format;
txt.x=50;
txt.y=200;
trace(myxml);
trace(mystr);
}
public function returntext(target:displayobjectcontainer):void
{
target.addchild(txt);
}
}//end of class
}//end of package
补充:flash教程,As3.0