4 Replies Latest reply on Aug 7, 2007 9:41 AM by marke

    Parsing RSS Feed with AS3 + E4X

    marke Level 1
      I am parsing out an rss feed from YouTube, but cannot seem to get Actionscript 3.0 to parse out the RSS tags that have colons in them (i.e. <media:thumbnail> OR <media:title>), I have provided the code.

      I'm hoping someone out there has a sollution!!???

      Cheers,
      Mark
        • 1. Re: Parsing RSS Feed with AS3 + E4X
          Level 7
          You'll need use a namespace.

          function loadLinks(myXML:XML):void{
          var media:Namespace = new Namespace(" http://search.yahoo.com/mrss/");
          default xml namespace = media;
          for each(var myLink:XML in myXML.channel.item){
          trace(myLink.thumbnail.@url);
          }
          }


          • 2. Re: Parsing RSS Feed with AS3 + E4X
            Level 7
            You can also use the "use namespace" directive:

            function loadLinks(myXML:XML):void{
            namespace media = " http://search.yahoo.com/mrss/";
            use namespace media;
            for each(var myLink:XML in myXML.channel.item){
            trace(myLink.thumbnail.@url);
            }
            }


            • 3. Re: Parsing RSS Feed with AS3 + E4X
              marke Level 1
              Thanks!
              • 4. Re: Parsing RSS Feed with AS3 + E4X
                marke Level 1
                Here is the updated version for folks to see. Thanks again Raymond.

                ////////////////// BEGIN ACTIONSCRIPT ///////////////////////

                import fl.controls.TileList;
                import fl.controls.ScrollBarDirection;

                // Create TileList instance
                var myTileList:TileList = new TileList();

                /**/
                var myScript:XML;
                var loader:URLLoader = new URLLoader();
                var request:URLRequest = new URLRequest(" http://www.youtube.com/rss/user/teamriverrunner/videos.rss");
                loader.load(request);
                loader.addEventListener(Event.COMPLETE, onComplete);

                function onComplete(event:Event):void{
                var loader:URLLoader = URLLoader(event.target);
                var myRSS:XML = new XML(loader.data);
                loadLinks(myRSS);
                }

                function loadLinks(myXML:XML):void{
                var media:Namespace = new Namespace(" http://search.yahoo.com/mrss/");
                //default xml namespace = media;
                for each(var myLink:XML in myXML.channel.item){
                myTileList.addItem({label:myLink.title, source:myLink.media::thumbnail.@url});
                }
                }

                // Set scroll bar direction
                myTileList.direction = ScrollBarDirection.VERTICAL;

                // create TextFormat object for labels
                var tf:TextFormat = new TextFormat();

                //set textFormat font, color, and size
                tf.font = "Verdana";
                tf.bold = true;
                //tf.color = 0x990000;
                tf.size = 10;

                //set styles
                myTileList.setStyle("contentPadding", -1);
                myTileList.setRendererStyle("textFormat", tf);
                myTileList.setRendererStyle("imagePadding", -1);

                // position TileList and set column and row values
                myTileList.move(0,0);
                myTileList.columnWidth = 158;
                myTileList.rowHeight = 115;
                myTileList.columnCount = 1;
                myTileList.rowCount = 2;
                myTileList.width = 158;
                myTileList.height = 284;

                //myTileList.width = myTileList.columnWidth * myTileList.columnCount + (myTileList.contentPadding * 2) + 15;// if scroll bar direction is vertical

                // Add to the display (Stage)
                addChild(myTileList);

                ////////////////// END ACTIONSCRIPT ///////////////////////