当前位置:软件学习 > Flash >>

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);
  };
 }
}; 

看上面的代码简单吧?

 

补充:flash教程,Action
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,