as读取xml文件到flash

今天来看看flash是怎么分析xml文档,并在flash文件中显示出来的,首页我们来看xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<channel>
<item>
<title>6场半全场第08032期推荐:罗马德比“和”...</title>
<link>/show.php?id=5</link>
<date>2008-03-22 00:00:00</date>
</item>
<item>
<title>6场半全场第08032期推荐:罗马德比“和”...</title>
<link>/show.php?id=6</link>
<date>2008-03-22 00:00:00</date>
</item>
<item>
<title>6场半全场第08032期推荐:罗马德比“和”...</title>
<link>/show.php?id=7</link>
<date>2008-03-22 00:00:00</date>
</item>
<item>
<title>6场半全场第08032期推荐:罗马德比“和”...</title>
<link>/show.php?id=8</link>
<date>2008-03-22 00:00:00</date>
</item>
<item>
<title>6场半全场第08032期推荐:罗马德比“和”...</title>
<link>/show.php?id=9</link>
<date>2008-03-22 00:00:00</date>
</item>
<item>
<title>6场半全场第08032期推荐:罗马德比“和”...</title>
<link>/show.php?id=10</link>
<date>2008-03-22 00:00:00</date>
</item>
</channel>

上面是我用php生成的xml文档,生成方法到本站去找,有教程.最重要的是as代码了,因为我们这次讲得是as 读出xml文件,所以就是最重要的喽.下面看as的代码,我们在flash里面建一个文件.

//建立xml对象
var myxml = new XML ();
var main:MovieClip = this;

//加载xml文件
myxml.load ("newslist.xml");
//忽略空格
myxml.ignoreWhite = true;//忽略空格 默认为false
//处理xml对象函数
newy = 20;
newx = 20;
mhj = 20;//行距
myxml.onLoad = function (success) {
node = this.firstChild.childNodes;
textNum = node.length;
for (i = 0; i < textNum; i++) {
   var new_mc = main.createEmptyMovieClip ("new_mc" + i, i);
   new_mc._x = newx;
   new_mc._y = newy + (mhj * i);
   new_mc.moveTo (newx,newy);
   new_mc.createTextField ("main",0,0,0,500,20);
   new_mc.main.html = true;
   new_mc.main.wordWrap = true;
   new_mc.main.text = String (myxml.childNodes[0].childNodes[i].childNodes[0].childNodes[0])+"       "+"[" + String (myxml.childNodes[0].childNodes[i].childNodes[2].childNodes[0]) + "]" ;
   trace (new_mc.main.text);

   //设置文字样式
   btntf = new TextFormat ();
   btntf.color = 0x000000;
   btntf.font = "verdana"; //字体型号
   btntf.size = 12;   //字体大小
   new_mc.main.setTextFormat (btntf);

   //按钮行为
   new_mc.i = i;
   new_mc.onRelease = function () {
    var who:Number = this.i;
    var link:String = myxml.childNodes[0].childNodes[who].childNodes[1].childNodes[0];
    trace (link);
    getURL (link, "_blank");
   };
   new_mc.onRollOver = function () {
    who=this.i
    this._y = newy + (mhj * who);
    menutf = new TextFormat ();
    menutf.color = 0xff9900;
    this.main.setTextFormat (menutf);
   };
   new_mc.onRollOut = function () {
    menutf = new TextFormat ();
    menutf.color = 0x0000;
    this.main.setTextFormat (menutf);
   };
}
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值