尝试写了个解析rss2.0的as类

      :: Flash :: 2005-10-20 12:55
rss2.0 和 1.0的区别还挺大的,5d还是1.0的
其中实践了EventDispatcher的用法,有点收获,代码写的并不算规范吧,用起来还行,觉得rss用类去解析反倒麻烦,因为用处不大。
由于很多节点名称太多,为了方便,直接封在object里,对照名称读取就可以

例子:
引用:

var myRss:RSSParser = new RSSParser();
var myObj:Object = new Object();
myObj.LoadStart = function() {
	if (myRss.Loaded) {
		trace("ok");
	}
};
var myObj2:Object = new Object();
myObj2.LoadComplete = function(obj) {
          var infos = myRss.Info;
          trace(infos.link);
          trace(myRss.itemLength);
          for (var i = 0; i<myRss.itemLength; i++) {
	 temp = myRss.Items[i];
	trace(temp.link+temp.title);
         }
};
myRss.addEventListener("LoadStart", myObj);
myRss.addEventListener("LoadComplete", myObj2);
myRss.LoadFile("data.xml");



class代码如下:
import mx.events.EventDispatcher;
class RSSParser{
  private var doc:String;  
  private var channel:Object;
  private var image:String;
  private var textinput:String;
  private var rssReader:XML;  
  public var Loaded:Boolean=true;
  
  private var rss:XMLNode;
  private var root:Object;
  public var Items:Array;
  public var itemLength:Number;

  function RSSParser(){     
    Items= new Array();
    root=this;
    channel={};
    rssReader=new XML();
    rssReader.ignoreWhite=true;
    //LoadFile();
    EventDispatcher.initialize(this);
  }
  public function toString(){
   return "Rss Reader";
  }
 
  function dispatchEvent(){}
  function addEventListener(){}
  function removeEventListener(){}

  public function LoadFile(_doc:String){
    doc= _doc;
    var t:Object=this;
    rssReader.load(doc);
    rssReader.onLoad=function(succecced){
      if(succecced){
        t.rss=this.firstChild.firstChild;
	t.parse();
      }else{
        Loaded=false;
      }
    }
    var eventObj1:Object = {target:this,type:"LoadStart"};
    dispatchEvent(eventObj1);
  }

  public function get Info(){ 
      return channel;
    }

  public function getImage(){
      if(!image){
       return false;
      }
      return image;
    }

  public function get TextInput(){
      if(!textinput){
       return false;
      }
      return textinput;
    }
  private function parse():Boolean{
     var node=rss.firstChild;
     while(node){
      if( node.nodeName == "image" )
        {
	 image=parseInfo(node);
        }
      else if( node.nodeName == "item" )
        {
        var item= parseItem(node);
        Items.push(item);        
        }
      else if( node.nodeName == "textinput" )
        {
        textinput= parseInfo(node);
        }else{
	  var __name=node.nodeName;
	  channel[__name]=node.firstChild.nodevalue;
	}
       node=node.nextSibling;
     }
    itemLength=Items.length;
    var eventObj2:Object = {target:this,type:"LoadComplete"};
    dispatchEvent(eventObj2);
    //Loaded=true;
    return true;
  }
  private function parseItem(item:XMLNode){
   var obj={};
   var node=item.firstChild;
   while(node){
     var __name=node.nodeName;
     obj[__name]=node.firstChild.nodevalue;
     node=node.nextSibling;
   }
   return obj;
  }

  private function parseInfo(item:XMLNode){
   var obj={};
   for (var str in item.attributes){
      obj[str]=item.attributes[str];
   }
   return obj;
  }
}
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}