as读取xml文件到flash
2012-11-24 09:31
435 查看
今天来看看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);
};
}
};
<?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读取XML文件
- flash读取外部xml配置文件基本方法
- FLASH读取JAVA生成的XML文件,不同平台乱码
- flash读取外部xml配置文件基本方法
- flash读取xml文件
- 我学Flash/Flex(2):AS3读取XML文件内容
- Flash读取XML文件最直接的方法
- flash动态读取xml输出的中文字会增加文件大小
- flash读取XML文件
- ActionScript 3.0 学习(十四) 从加载的.swf文件中读取flash库中的as链接的类
- EasyPlayerPro Windows播放器读取xml配置文件中的特殊字符问题
- flash跨域策略文件crossdomain.xml配置详解
- 读取XML文件之DOM官方方法
- 如何从XML文件中读取数据绑定到DropDownList
- android编程:xml文件读取和写入
- java读取xml配置文件(小结)
- GTK+-3.0编程 (五) GtkBuilder用于快速创建界面,它读取一个xml文件
- 深入浅出学习Struts1框架(七):ActionServlet实例化之读取struts-config.xml配置文件
- hibernate4的cfg.cml文件和hbm.xml文件无法读取报错
- 读取SPRING XML配置文件中的hbm.xml 文件列表.