Question

I'm trying to read the iTunes XML file with PHP. I want to extract particular bits of information. Such as the Name, Artist and Album. How would I do this? I found this which works quite well when parsing the whole file. I thought perhaps using this to create a whole new XML file which is easier to read.

But there must be a better way.

The XML structure for a track looks like this:

<dict>
    <key>Track ID</key><integer>6136</integer>
    <key>Name</key><string>The Boy Who Destroyed the World</string>
    <key>Artist</key><string>AFI</string>
    <key>Album Artist</key><string>AFI</string>
    <key>Composer</key><string>AFI</string>
    <key>Album</key><string>Tony Hawk Pro Skater 3</string>
    <key>Genre</key><string>Punk Rock</string>
    <key>Kind</key><string>MPEG audio file</string>
    <key>Size</key><integer>2971924</integer>
    <key>Total Time</key><integer>185364</integer>
    <key>Track Number</key><integer>3</integer>
    <key>Year</key><integer>1999</integer>
    <key>Date Modified</key><date>2009-08-20T15:03:20Z</date>
    <key>Date Added</key><date>2009-08-20T15:03:20Z</date>
    <key>Bit Rate</key><integer>128</integer>
    <key>Sample Rate</key><integer>44100</integer>
    <key>Play Count</key><integer>34</integer>
    <key>Play Date</key><integer>3332385360</integer>
    <key>Play Date UTC</key><date>2009-08-06T05:36:00Z</date>
    <key>Sort Name</key><string>Boy Who Destroyed the World</string>
    <key>Persistent ID</key><string>9E590180768D4AD8</string>
    <key>Track Type</key><string>File</string>
    <key>Location</key><string>FilePath</string>
    <key>File Folder Count</key><integer>4</integer>
    <key>Library Folder Count</key><integer>1</integer>
</dict>
Was it helpful?

Solution

I don't think parsing the file and then creating another file which is easier to parse is the right solution. You'll have already parsed the main file, so why make more work?

simplexml allows you to navigate an XML object with relative ease, so you should be able to get the information you want. I would usesimplexml_load_file() or simplexml_load_string() to load the main file, and then probably SimpleXMLElement::xpath to navigate through your resulting XML structure to find the data you want.

OTHER TIPS

SimpleXML is the builtin way to do this. http://www.devshed.com/c/a/XML/SimpleXML/ seems like a good tutorial, as well as the php.net section on simpleXML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top