Parse iTunes XML with a couple lines of PHP

You may recall I half-wrote a script to do this a couple years ago, but I was mostly cribbing from some guy at Apple and really had no idea what i was doing.

Anyway, I’m taking another shot at parsing the iTunes library XML using PHP5’s new “SimpleXML” library, which is a terrific feature that I never get tired of working with.

I cooked up this little script in about 10 minutes. The format of Apple’s XML files makes it sort of a pain to process in the usual ways, but you can still get reasonable output with just a few lines of code. And most of that code is concerned with formatting, not the actual XML processing.

  • the code
  • the output
  • the sample source file

To use this script:

  1. Make sure you are using PHP5 with SimpleXML support. At Dreamhost, you can turn this on by clicking the “edit” link on the main domain management page. If you’re hosting a site on your Mac, you can get an easy-to-install version of PHP5.
  2. In iTunes, select a Playlist or Library, choose “File:Export…” and export it as XML. Name it itunes.xml. (If your site is hosted on your Mac, you can just create a symlink called “itunes.xml” that points to your “iTunes Music Library.xml” file or the playlist file you just exported. Place this link in your website directory.)
  3. Upload itunes.phps and itunes.xml to your webserver.
  4. Rename itunes.phps to itunes.php.
  5. Open your browser and navigate to itunes.php.

Obviously there are lots of ways to enhance the functionality of this script, but this will get you started.

Leave a Reply

Your email address will not be published. Required fields are marked *