【www.gdgbn.com--同学】

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.111cn.net/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 = "

" + rrstitle + "



";
 
   for each(var item:xml in xmlcontent..item)
   {
    var itemtitle:string = item.title.tostring();
    var itemlink:string = item.link.tostring();
    temp.htmltext += "

" + itemtitle + "



";
   }
  }
 }
}

实例二,读取外部xml 文件


  

 
          
          
          
          
 

 

------------------------------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.sprite

 public 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

本文来源:http://www.gdgbn.com/zhufuduanxin/29746/