iTunes XML Parser for PHP

download open source project site

This PHP script function will take an xml file from iTunes "Export Song List..." under the "File" menu and converts it into a usable array. It will also sort the array if you want. This is cool, not just 'cause I wrote it, but because it rocks. I have released it under the Lesser GNU Public License (LGPL) instead of the GPL so anyone can use it even in a commercial product.

Peter Minarik has modified the script to use DomDocument() instead of domxml_open_mem() etc... so that the script works correctly with PHP 5 using the new DOM functions.

Syntax

array iTunesXmlParser (string filename [, string sort_field=NULL [, string sort_direction="up"]])

Quick Example

// must do this, include itunes_xml_parser_php5.php if your server is php v5
include "itunes_xml_parser.php";

// get songs from the xml file
$songs = iTunesXmlParser("techno_playlist.xml");

// if it worked
if ($songs)
{
	// create a table
	$output = '<table cellpadding="0" cellspacing="0" border="0" width="100%">';

	// fill the first row with headings
	$output .= '<tr>';
	$output .= '<td><font size="1">name</font></td>';
	$output .= '<td><font size="1">artist</font></td>';
	$output .= '<td><font size="1">album</font></td>';
	$output .= '<td><font size="1">rating</font></td>';
	$output .= '</tr>';

	// loop through the songs in the array and get 4 fields that I want to see
	foreach ($songs as $song)
	{
		$output .= '<tr>';
		$output .= '<td><font size="2">'.$song["Name"].'</font></td>';
		$output .= '<td><font size="2">'.$song["Artist"].'</font></td>';
		$output .= '<td><font size="2">'.$song["Album"].'</font></td>';
		$output .= '<td><font size="2">'.$song["Rating"].'</font></td>';
		$output .= '</tr>';
	}

	// end the table
	$output .= '</table>';

	// show my new table
	print ($output);
}