Digging Into the iTunes Library File

I've already mentioned several times that the iTunes library file is stored in an XML format but I haven't given you any clues regarding how to find or access the file. The file is stored in a different location depending on whether you're using a Windows or Macintosh computer. Either way, the file is named iTunes Music Library.xml. Following are the locations where you can find the file on each type of computer:

  • Windows My Documents/My Music/iTunes/iTunes Music Library.xml

  • Mac Music/iTunes/iTunes Music Library.xml

If, for some reason, you have trouble finding the iTunes XML library file or if you're just too lazy to drill down into the folders to find it, there is a simpler alternativejust export the file directly from within iTunes. Click File on the main iTunes menu, followed by Export Library. You'll be given an opportunity to specify the location and filename of the exported XML library file. When you have the library file handy, you can begin studying it and figuring out how to manipulate the XML code for your own needs.

Listing 13.1 contains an excerpt from an iTunes library file. Keep in mind that there is a lot more information in the library file that I've left out. The emphasis for this lesson is on extracting XML data pertaining to the tracks referenced in the library, so the listing only shows the XML code for a single track.

Listing 13.1. The XML Code for a Single Track Within an iTunes Library File
 1: <dict>
 2:   <key>Track ID</key><integer>35</integer>
 3:   <key>Name</key><string>Landslide</string>
 4:   <key>Artist</key><string>Fleetwood Mac</string>
 5:   <key>Album</key><string>The Dance</string>
 6:   <key>Genre</key><string>Rock</string>
 7:   <key>Kind</key><string>Protected AAC audio file</string>
 8:   <key>Size</key><integer>4285488</integer>
 9:   <key>Total Time</key><integer>268283</integer>
10:   <key>Disc Number</key><integer>1</integer>
11:   <key>Disc Count</key><integer>1</integer>
12:   <key>Track Number</key><integer>9</integer>
13:   <key>Track Count</key><integer>17</integer>
14:   <key>Year</key><integer>1997</integer>
15:   <key>Bit Rate</key><integer>128</integer>
16:   <key>Sample Rate</key><integer>44100</integer>
17: </dict>s

Based upon what you've learned, you've probably already realized that this isn't your average run-of-the-mill XML code. In fact, Apple deviated considerably from traditional XML design sense by structuring the iTunes library file as pairs of key values instead of more meaningful XML tags. In other words, just about everything in an iTunes library is stored as a key with a corresponding value. As an example, the artist for the track is coded using a <key> tag with Artist as its content (line 4). Immediately following this <key> tag is a corresponding <string> tag that holds the name of the artist associated with the key; in this case, Fleetwood Mac.

You can continue examining the code in the listing to see how other information about the track is coded using key values. The key-value pairings always consist of a <key> tag followed by a tag that indicates the type of the data stored, such as <string> or <integer>. An example of an integer key-value is the track number, which is the number of the track on the actual album or CD (line 12). In this example, the song "Landslide" is track number 9 on the CD The Dance by Fleetwood Mac.

There is a DTD available from Apple for validating iTunes music library files. You can access the DTD online at http://www.apple.com/DTDs/PropertyList-1.0.dtd.

Taking a step back for a moment, it's worth noting that the key-value information for a track occurs within a dict element. dict stands for dictionary, which simply refers to a data structure consisting of keys and values. A complete library file consists of multiple nested dict elements that are used to store various pieces of information related to the library, such as the list of tracks in the library along with any playlists. Because you're only concerned at the moment with extracting and viewing the library of songs, it isn't necessary to deal with playlist data.

You now have some idea regarding the structure of the iTunes library file but there is another topic worth addressing before you start ripping through the XML code to generate a view of it that is browser friendly. I'm referring to the creation of links to the iTunes music store.