Tuesday, January 18, 2011

Itunes XML Parser

This simple php code will help you to parse the Itunes XML so that we can grab whichever data we need.

<?php
$myFile = "itunes.xml"; //XML Path
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

$artist = array();
$start = explode('<key>Artist</key><string>',$theData); //Change this to which key you want
for($i=1;$i<count($start);$i++)
{
    $end = explode('</string>',$start[$i]);
    array_push($artist,$end[0]);
}
$artistList = array_unique($artist);
print_r($artistList );
?>


Sample XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Major Version</key><integer>1</integer>
    <key>Minor Version</key><integer>1</integer>
    <key>Application Version</key><string>9.0.1</string>
    <key>Features</key><integer>5</integer>
    <key>Show Content Ratings</key><true/>
    <key>Music Folder</key><string>file://localhost/C:/Users/Backup/Backup/My%20Music/iTunes/iTunes%20Music/</string>
    <key>Library Persistent ID</key><string>F4C41DD367C50FF8</string>
    <key>Tracks</key>
    <dict>
        <key>1191</key>
        <dict>
            <key>Track ID</key><integer>1191</integer>
            <key>Name</key><string>futures</string>
            <key>Artist</key><string>Jimmy Eat World</string>
            <key>Album</key><string>Futures</string>
            <key>Genre</key><string>Rock</string>
            <key>Kind</key><string>MPEG audio file</string>
            <key>Size</key><integer>6128689</integer>
            <key>Total Time</key><integer>240509</integer>
            <key>Track Number</key><integer>1</integer>
            <key>Year</key><integer>2004</integer>
            <key>Date Modified</key><date>2004-09-10T22:49:02Z</date>
            <key>Date Added</key><date>2005-10-04T00:38:48Z</date>
            <key>Bit Rate</key><integer>203</integer>
            <key>Sample Rate</key><integer>44100</integer>
            <key>Comments</key><string>-=KSi=-  boozy  -=KSi=-</string>
            <key>Persistent ID</key><string>372A8EE2B07ABD84</string>
            <key>Track Type</key><string>File</string>
            <key>Location</key><string>file://localhost/C:/Documents%20and%20Settings/My%20Documents/My%20Music/Jimmy%20Eat%20World%20-%20Futures/01-jimmy_eat_world-futures-ksi.mp3</string>
            <key>File Folder Count</key><integer>-1</integer>
            <key>Library Folder Count</key><integer>-1</integer>
        </dict>
        <key>1193</key>
        <dict>
            <key>Track ID</key><integer>1193</integer>
            <key>Name</key><string>just tonight...</string>
            <key>Artist</key><string>Jimmy Eat World</string>
            <key>Album</key><string>Futures</string>
            <key>Genre</key><string>Rock</string>
            <key>Kind</key><string>MPEG audio file</string>
            <key>Size</key><integer>5316928</integer>
            <key>Total Time</key><integer>208587</integer>
            <key>Track Number</key><integer>2</integer>
            <key>Year</key><integer>2004</integer>
            <key>Date Modified</key><date>2004-09-10T22:49:12Z</date>
            <key>Date Added</key><date>2005-10-04T00:38:48Z</date>
            <key>Bit Rate</key><integer>203</integer>
            <key>Sample Rate</key><integer>44100</integer>
            <key>Comments</key><string>-=KSi=-  boozy  -=KSi=-</string>
            <key>Persistent ID</key><string>372A8EE2B07ABD85</string>
            <key>Track Type</key><string>File</string>
            <key>Location</key><string>file://localhost/C:/Documents%20and%20Settings/My%20Documents/My%20Music/Jimmy%20Eat%20World%20-%20Futures/02-jimmy_eat_world-just_tonight___-ksi.mp3</string>
            <key>File Folder Count</key><integer>-1</integer>
            <key>Library Folder Count</key><integer>-1</integer>
        </dict>
</plist>

No comments:

Popular Posts