Thursday, 4 June 2009

AS file to parse the Picasa Album atom feed

I've been asked so many times for the script to parse the atom feed provided by the picasa api for the album. The script I am posting here is simple and can be used to parse the feed for the album. Please feel free to change to your need.



/*
* PicasaAlbumParser.as
* File to parse Picasa album xml retrieved from http://picasaweb.google.com/data/feed/api/user/{username}
*
* Jatin Desai
* desaijatin at gmail.com
*
* File converts xml to array and each element contains album object.
* This class extracts the information needed for this project but feel free to change. This class was
* initially derived from Mike Chambers' website (parsing atom feeds)
* http://www.mikechambers.com/blog/2004/02/05/atom-feed-actionscript-2-class-alpha/
*
*/

package
{
import flash.xml.XMLDocument;
import flash.xml.XMLNode;

public class PicasaAlbumParser
{
public function PicasaAlbumParser()
{
}

public function ParseAlbumList( xml:XML ):Array
{
trace("*********************************************");
trace("PARSING ATOM FEED");
trace("*********************************************");
var result:XMLDocument = new XMLDocument();
var arAlbum:Array = new Array();

result.parseXML( xml.toXMLString() );

/* get the array of nodes withing response xml */
var nodes:Array = result.firstChild.childNodes;

for each( var node:XMLNode in nodes ) {
switch( node.nodeName )
{
/* we are only interested in entry node */
case "entry":
arAlbum.push( ParseAlbumEntries(node) );
break;
}
}

/* Return album */
return arAlbum;
}

/* Parse album entries node */
private function ParseAlbumEntries(entry:XMLNode):Object
{
var album:Object = new Object();
var nodes:Array = entry.childNodes;

for each( var node:XMLNode in nodes )
{
switch( node.nodeName )
{
case "id":
album.id = node.firstChild.nodeValue;
break;
case "title":
album.title = node.firstChild.nodeValue;
break;
case "gphoto:id":
album.albumid = node.firstChild.nodeValue;
break;
case "gphoto:name":
album.albumname = node.firstChild.nodeValue;
break;
case "gphoto:numphotos":
album.numphotos = node.firstChild.nodeValue;
break;
case "media:group":
album.media = ParseAlbumMediaGroup(node);
break;
}
}

/* return the album object */
return album;
}

private function ParseAlbumMediaGroup(entry:XMLNode):Object
{
var media:Object = new Object();
var nodes:Array = entry.childNodes;

for each( var node:XMLNode in nodes )
{
switch( node.nodeName )
{
case "media:description":
media.description = "";
if(node.firstChild != null)
media.description = node.firstChild.nodeValue;
break;
case "media:thumbnail":
media.thumbnail = node.attributes.url;
break;
case "media:content":
media.bigimage = node.attributes.url;
break;
}
}

/* Return media object */
return media;
}
}
}



Thanks,

Jatin

0 comments: