Use PHP to Read an iTunes Music Library XML file
I've been working on a web based music streaming server and I kept running into a common complaint, that the parsing the iTunes file with an off the shelf plist reader is a resource hog. Well it doesn't have to be! PHP has a fantastic built in tool for dealing with the iTunes plist serialization format that will once and for all parse your library in the most resource friendly way possible.
Apple has a resonable demonstration of copying an itunes file directly to memory in Apple's PHP hints , but there are some major issues here. Most of the code is in PHP4 with a lot of outdated, procedural techniques that leaves much of the program on the global scope. The real gem here is the XPAT/SAX (Simple Api for XML) example, because of an important consideration - it can stream through an XML file instead of preloading it. So the goal here is to make a PHP5 compliant, OOP friendly, streaming iTunes Music Library.xml reader - so let's go!
This is what we want. It works a little like the fetch_row methods for mysql: while there are still records, keep on flushing them out to your database, application's output buffer or cache. Each loop destroys the previous array to conserve memory and work extremely quickly. This nice frontend has a fairly short and sweet class accompanying it:
So let's have a quick at the insides of this class. XML serialization and parsing is a job handled fairly easily by most programming languages nowadays, but PLIST ( Apple's property list definitions ) is a very strange duck. each key and value are surrounded in their own tags, so you end up having to make sens of pairs that aren't directly related except by adjacency <dict><key>Name</key><string>08 - Untitled 8</string>...</dict>. What this class does is it reads the XML line by line and fires off events when it thinks its found useful data. when these events occur, you can reorganize the output into a familiar associative array.
Here's what the output looks like for each song:
The events key to breaking up this file are <dict>, the elements that start and end the items; <key/><type/> the values that break up the key value pairs and <playlist>, which signifies the end of all of the tracks in the file. The SAX implementation in PHP isn't too bad - it gives the developer a line by line XML parser with all the smarts of a generalized XML parser like simplexml, so you won't end up being left to handle string manipulation. I've actually gone through and tested Apple's example code and made quite a few changes to their original to handle UTF-8 characters and to make it fit into an object oriented project a little nicer. If you have questions about any of this stuff, just leave a comment below.
This code example is part of the Streeme music server project. If you're looking for a free and open source way to stream your music library over the web, check out Streeme on Google Code or on Github!
