<?
//for xml parsing of itunes XML file
include "parseXML_functions.php";
$file="../library.xml"; //file containing xml iTunes library
$xml_parser="";
$songs=array(); //will hold each song in a 2-d array
$number_dicts=0; //counter, number of 'dict' elements encountered
$current_key=""; //key for each element in second dimension of array
$current_element=""; //stores xml element name
$current_data=""; //value for second dimension array elements
$end_of_songs=FALSE; //boolean used to help let us know if we're
//done with the song list
function start_element($parser, $name, $attribs) {
global $current_element, $number_dicts;
if($name=="DICT"){
$number_dicts++;
}
if ($number_dicts>2){
$current_element=$name;
}
}
function end_element($parser, $name) {
global $songs, $current_element, $current_data, $number_dicts, $array_key, $end_of_songs;
if($end_of_songs){
return;
}
if(!empty($current_element)) {
if($current_element=="KEY"){
$array_key=$current_data;
}else{
$songs[$number_dicts][$array_key]=$current_data;
}
}
}
function character_data($parser, $data) {
global $number_dicts, $current_data, $end_of_songs;
if($data=="Playlists") {
$end_of_songs=TRUE;
}
$current_data=trim($data);
}
function refreshLibrary() {
$file = "../library.xml";
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "start_element", "end_element");
xml_set_character_data_handler($xml_parser, "character_data");
if (!($fp = @fopen($file, "r"))) {
echo "could not open library: " . $file;
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
}
?>