/**
* flash读取XML
* author: cowoo
* date: 2004.7.28
*/
这里我以我网站中的一个部分为例子来讲解一下flash中如何读取XML文件:
打开site->home->home.fla,你一定注意到了,stage的中间有两个MC,为了路径操作的
方便,我将读取XML的代码直接写到了这两个MC上,当然,也可以直接创建一个空的MC,
然后在上面贴上代码,但是路径的问题可能会困扰一些人。
首先看一下这个XML文件的结构:
<?xml version = "1.0" encoding = "gb2312"?>
<updatelist>
<update>update1</update>
<update>update2</update>
<update>update3</update>
</updatelist>
代码如下,我在必要的地方加上了注释:
onClipEvent(load)
{
System.useCodepage = true; //中文
update_xml = new XML(); //声明一个XML对象
update_xml.ignoreWhite = true; //忽略XML中的空白
update_xml.load("home/update.xml"); //因为这个文件将被加载到site->site.swf(也就是主影片)中去,所以读取xml的路径是home/update.xml
update_xml.onLoad = function(success) //开始加载xml文件
{
if(success) //如果成功的话执行以下操作
{
UpdateInfo = update_xml.firstChild; //读取进来的XML文件的firstChild是<updatelist>
Publish(UpdateInfo);
delete update_xml;
sb1.setScrollTarget(tf1); //使文本刷新后的scrollbar仍然可用
sb2.setScrollTarget(tf2);
sb3.setScrollTarget(tf3);
}
else
_parent.update1 = "Error loading XML...";
}
function Publish(xml_doc)
{
content = xml_doc.childNodes; //这个content有点像一个数组用来存储<updatelist>的子结点们
for(i = 0;i < 3;i++)
{
this["update" add (i+1)] = content[i].firstChild; //update1等是影片剪辑中的动态文本框的变量。注意content[i].firstChild才是文本,即文本也被当作一个结点
}
}
}
基本的工作就是这样,如果我这篇文章有什么问题的话欢迎批评指正。
--------------------------------------------------------------------------------
我上次那个网站就是全部用XML读的文件,我今晚上写一个比较简单的教程大家看一下,
源文件可到我的网站上去下载。
http://www.cowoo.net
Sorry,服务器出了点问题,点击下面的下载:
http://software.hit.edu.cn/home/cowoo/site.rar
可以通过http://software.hit.edu.cn/home/cowoo/index预览。