Version: 1.0 Beta
Type: Class
Category: Other
License: GNU General Public License
Description: This class parses iTunes playlists into a multidimensional array ... You can find the class + a sample index.php file at http://www.imike.us/itunesxml.zip.
<?
//
// iTunes XML Playlist Parser Class
// Mike Pacific
// imike@deskmod.com
// http://www.deskmod.com
//
//
// How to Use:
// First, activate the parser by calling the initial function (ie. $playlist_parser->parser($playlist_file); where $playlist_file
// is the path to your xml file. After this, the information is ready. First off, the playlist title is stored in a variable
// called $playlist_title (ie. $playlist_parser->playlist_title). There is also an array called $playlist which is structured as follows:
// $playlist[artist name][increment counter][album/title/genre/time]
// if you have any questions, please e-mail me
//
class playlist_parser
{ var $xml_parser;
var $xml_current;
var $xml_key;
var $xml_cursong;
var $xml_curartist;
var $title_key;
var $counter=1;
var $playlist_title;
var $playlist=array();
function xml_start($xml_parser,$xml_tag,$xml_attributes)
{ $this->xml_current.="/~".$xml_tag;
}
function xml_end($xml_parser,$xml_tag)
{ $xml_caret_pos=strrpos($this->xml_current,"/~");
$this->xml_current=substr($this->xml_current,0,$xml_caret_pos);
}
function xml_data($xml_parser,$xml_data)
{ if($this->xml_current=="/~PLIST/~DICT/~DICT/~DICT/~KEY"&&$xml_data=="Track ID")
{ $this->counter++;
}
switch($this->xml_current)
{ case"/~PLIST/~DICT/~DICT/~DICT/~KEY":
$this->xml_key=$xml_data;
break;
case"/~PLIST/~DICT/~DICT/~DICT/~STRING":
switch($this->xml_key)
{ case"Name":
$this->xml_cursong=$xml_data;
break;
case"Artist":
$this->playlist[$xml_data][$this->counter]['song']=$this->xml_cursong;
$this->xml_curartist=$xml_data;
break;
case"Album":
$this->playlist[$this->xml_curartist][$this->counter]['album']=$xml_data;
break;
case"Genre":
$this->playlist[$this->xml_curartist][$this->counter]['genre']=$xml_data;
break;
default:
break;
}
break;
case"/~PLIST/~DICT/~DICT/~DICT/~INTEGER":
switch($this->xml_key)
{ case"Total Time":
$this->playlist[$this->xml_curartist][$this->counter]['time']=$xml_data;
break;
}
break;
case"/~PLIST/~DICT/~ARRAY/~DICT/~KEY":
$this->title_key=$xml_data;
break;
case"/~PLIST/~DICT/~ARRAY/~DICT/~STRING":
switch($this->title_key)
{ case"Name":
if(!$this->playlist_title)
{ $this->playlist_title=$xml_data;
}
break;
default:
break;
}
break;
default:
break;
}
}
function parser($playlist_file)
{ $this->xml_parser=xml_parser_create();
xml_set_object($this->xml_parser,&$this);
xml_set_element_handler($this->xml_parser,"xml_start","xml_end");
xml_set_character_data_handler($this->xml_parser,"xml_data");
$xml_file=fopen($playlist_file,"r");
while($xml_data=fread($xml_file,4096))
{ xml_parse($this->xml_parser,$xml_data,feof($xml_file));
}
ksort($this->playlist);
}
}
?>