如何在PHP中阅读iTunes XML文件

时间:2011-01-26 20:00:39

标签: php xml simplexml

出于某种原因,iTunes决定将非标准XML格式与其文件一起使用......因此我无法使用SimpleXMLElement阅读它......

iTunes 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>10.1.1</string>
    <key>Features</key><integer>5</integer>
    <key>Show Content Ratings</key><true/>
    <key>Music Folder</key><string>file://localhost/D:/iTunes/Bob%20Russell%20Sermons/iTunes%20Media/</string>
    <key>Library Persistent ID</key><string>B8EB1073DB45BBA1</string>
    <key>Tracks</key>
    <dict>
        <key>290</key>
        <dict>
            <key>Track ID</key><integer>290</integer>
            <key>Name</key><string>01-03 Good News</string>
            <key>Artist</key><string>Bob Russell</string>
            <key>Album</key><string>Not in Series</string>
            <key>Grouping</key><string>Gospel, Decision Day, Salvation, Sin, Commandments, Mercy, Grace, Jesus Christ, Crucifixion, Resurrection, Justice, Hope</string>
            <key>Genre</key><string>Speech</string>
            <key>Kind</key><string>MPEG audio file</string>
            <key>Size</key><integer>18140686</integer>
            <key>Total Time</key><integer>1774132</integer>
            <key>Year</key><integer>1999</integer>
            <key>Date Modified</key><date>2010-09-09T17:41:58Z</date>
            <key>Date Added</key><date>2011-01-18T19:31:43Z</date>
            <key>Bit Rate</key><integer>80</integer>
            <key>Sample Rate</key><integer>44100</integer>
            <key>Comments</key><string>John 3:16-21; Light has come into the world, but men loved darkness; Romans 1:20; gather you as a mother hen gather chicks;1 Corinthians 15:19</string>
            <key>Artwork Count</key><integer>1</integer>
            <key>Persistent ID</key><string>7B3B348BF9BB95CE</string>
            <key>Track Type</key><string>File</string>
            <key>Location</key><string>file://localhost/D:/iTunes/Bob%20Russell%20Sermons/Sermons/1999/01-03%20Good%20News.mp3</string>
            <key>File Folder Count</key><integer>3</integer>
            <key>Library Folder Count</key><integer>1</integer>
        </dict>

通过SimpleXML输出:

SimpleXMLElement Object
(
    [key] => Array
        (
            [0] => Track ID
            [1] => Name
            [2] => Artist
            [3] => Album
            [4] => Genre
            [5] => Kind
            [6] => Size
            [7] => Total Time
            [8] => Year
            [9] => Date Modified
            [10] => Date Added
            [11] => Bit Rate
            [12] => Sample Rate
            [13] => Comments
            [14] => Artwork Count
            [15] => Persistent ID
            [16] => Track Type
            [17] => Location
            [18] => File Folder Count
            [19] => Library Folder Count
        )

    [integer] => Array
        (
            [0] => 332
            [1] => 6419273
            [2] => 1415262
                [3] => 1991
            [4] => 32
            [5] => 22050
            [6] => 1
            [7] => 3
            [8] => 1
        )

    [string] => Array
        (
            [0] => 07-07 Thank God for America
            [1] => Bob Russell
            [2] => No Series
            [3] => Speech
            [4] => MPEG audio file
            [5] => Isaiah 40:13-26
            [6] => 94BD9A32817C24F1
            [7] => File
            [8] => file://localhost/D:/iTunes/Bob%20Russell%20Sermons/Sermons/1999/07-07%20Thank%20God%20for%20America.mp3
        )

    [date] => Array
        (
            [0] => 2010-05-28T03:27:34Z
            [1] => 2011-01-18T19:31:47Z
        )

)

我遇到了问题,因为如果缺少信息,数组键会不一致。在一个语音中它可能有8个整数值,在另一个语音上它可能有5个......对我来说读这些数据的更好的方法是什么?

2 个答案:

答案 0 :(得分:9)

这是plist XML file。 Apple使用它们很多。我建议找一个将plist文件解析成适当的本机数据结构的库。 plistParser看起来很有用。

plistParser示例:

<?php
include("PlistParser.inc");

$parser = new plistParser();
$plist = $parser->parse(dirname(__FILE__) . "/iTunes.xml");
var_dump($plist);
?>

输出:

array(8) {
  ["Major Version"]=>
  int(1)
  ["Minor Version"]=>
  int(1)
  ["Application Version"]=>
  string(5) "8.0.1"
  ["Features"]=>
  int(5)
  ["Show Content Ratings"]=>
  bool(true)
  ["Music Folder"]=>
  string(60) "file://localhost/Users/testUser/Music/iTunes/iTunes%20Music/"
  ["Library Persistent ID"]=>
  string(15) "C39203948AF3D3E"
  ["Tracks"]=>
  array(1) {
    [1]=>
    array(25) {
      ["Track ID"]=>
      int(1)
      ["Name"]=>
      string(8) "My Track"
      ["Artist"]=>
      string(9) "My Artist"
      ["Album"]=>
      string(8) "My Album"
      ["Genre"]=>
      string(8) "My Genre"
      ["Kind"]=>
      string(15) "MPEG audio file"
      ["Size"]=>
      int(123456)
      ["Total Time"]=>
      int(123456)
      ["Track Number"]=>
      int(1)
      ["Year"]=>
      int(2008)
      ["Date Modified"]=>
      string(20) "2008-03-03T03:33:33Z"
      ["Date Added"]=>
      string(20) "2008-03-03T03:33:33Z"
      ["Bit Rate"]=>
      int(128)
      ["Sample Rate"]=>
      int(44100)
      ["Comments"]=>
      string(20) "All Rights Reserved."
      ["Release Date"]=>
      string(20) "2007-03-12T04:01:37Z"
      ["Persistent ID"]=>
      string(14) "C3E339393939E3"
      ["Track Type"]=>
      string(4) "File"
      ["Podcast"]=>
      bool(false)
      ["Unplayed"]=>
      bool(true)
      ["File Type"]=>
      int(123456)
      ["File Creator"]=>
      int(123456)
      ["Location"]=>
      string(66) "file://localhost/Users/testUser/Music/iTunes/iTunes%20Music/my.mp3"
      ["File Folder Count"]=>
      int(4)
      ["Library Folder Count"]=>
      int(1)
    }
  }
}

答案 1 :(得分:2)

有点晚了,但是我对我的项目使用了这个确切的建议,我使用了plist解析器近一年。最大的问题是它是一种通用的Plist解析解决方案,非常适合配置和二进制文件,但它不使用数百兆字节的RAM就无法处理大型iTunes库。我最近为php创建了一个基于SAX的解析器,它可以正确地通过iTunes文件流,在每个歌曲条目上提供格式正确的数组。

包括你需要的Github要点的文章在这里:

http://streeme.posterous.com/how-to-read-an-itunes-music-library-xml-file