Code Triangle™ Custom Software Development
homeservicessupportexperimentscontactabout
iTunes XML parser for PHP
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.
Definition
array iTunesXmlParser (string filename [, string sort_field=NULL [, string sort_direction="up"]])
Parameters
Takes the filename of an XML playlist file exported from Apple's iTunes music player. Then it returns an array of arrays with all the information you need.
You may also specify a sort_field such as "Name" and a direction to sort the list (sort_direction) "up" or "down".

If no sort_field is specified, iTunesXmlParser() leaves the array unsorted.
Requirements
The DOM XML extension to PHP 4.2.1 or greater.

Downloads

download filname size date
iTunes XML PHP Parser for PHP 5 (by: Peter Minarik)iTunesXmlParser5.zip15 kbJun 19, 2007
iTunes XML PHP ParseriTunesXmlParser.zip15 kbJun 19, 2007

Live Example

The all-mighty Code Triangle Music Play-list featuring the above script. And not one, but two whole sortable playlists!

Example

<?php
include "itunes_xml_parser.php";
$array = iTunesXmlParser("My Top Rated.xml", "Name", "down");
print_r($array);
?>
	
Output
Array (
	[0] => Array 
		( 
			[Track ID] => 34
			[Name] => depeche mode gameboy megamix!
			[Artist] => nullsleep
			[Composer] => nullsleep
			[Album] => www.nullsleep.com
			[Genre] => Electronic
			[Kind] => MPEG audio file
			[Size] => 13901952
			[Total Time] => 868780
			[Year] => 2002
			[Date Modified] => 2002-12-17T10:24:18Z
			[Date Added] => 2003-11-11T06:38:24Z
			[Bit Rate] => 128
			[Sample Rate] => 44100
			[Comments] => www.8bitpeoples.com 1. ...
			[Play Count] => 3
			[Play Date] => -1142117754
			[Play Date UTC] => 2003-11-28T15:32:22Z
			[Rating] => 80
			[Normalization] => 976
			[Location] => file://localhost/C:/med ...
			[File Folder Count] => 4
			[Library Folder Count] => 1
		)
	[1] => Array
		(
			[Track ID] => 65
			[Name] => Daftendirekt
			[Artist] => Daft Punk
			[Album] => Homework
			[Genre] => Electronic
			[Kind] => MPEG audio file
			[Size] => 4098756
			[Total Time] => 164649
			[Track Number] => 1
			[Date Modified] => 2003-11-28T19:45:05Z
			[Date Added] => 2003-11-11T06:38:25Z
			[Bit Rate] => 192
			[Sample Rate] => 44100
			[Play Count] => 5
			[Play Date] => -1142119790
			[Play Date UTC] => 2003-11-28T14:58:26Z
			[Rating] => 80
			[Normalization] => 1414
			[Location] => file://localhost/C:/media/ ...
			[File Folder Count] => 4
			[Library Folder Count] => 1
		)
}
	

iTunes is a product by Apple Computer, Inc.