Reading iTunes XML file with PHP

I am trying to read an iTunes XML file from PHP. I want to extract certain bits of information. For example, name, artist and album. How should I do it? I found this one that works great when parsing the entire file. I was thinking maybe using this to create a completely new XML file that is easier to read.

But there must be a better way.

The XML structure for the 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>

      

+2


source to share


2 answers


I don't think parsing the file and then creating another file that is easier to parse is the correct solution. You've already analyzed the main file, so why do more work?



simplexml makes it easy to navigate the XML object so you can get the information you want. I would use simplexml_load_file()

either simplexml_load_string()

to load the main file, and then probably SimpleXMLElement :: xpath to navigate through your resulting XML structure to find the data I want.

+1


source


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



0


source







All Articles