如何用 PHP 读取 iTunes XML 文件
由于某种原因 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 个……对我来说,读取这些数据的更好方法是什么?
For some reason iTunes has decided to use non-standard XML formats with their files... so I'm having trouble reading it with SimpleXMLElement...
iTunes XML sample:
<?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>
Output via 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
)
)
I'm running into issues because array keys are inconsistent if information is missing. On one speech it may have 8 integer values, on another it may have 5.... What's a better way for me to read this data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 plist XML 文件。苹果经常使用它们。我建议找到一个将 plist 文件解析为适当的本机数据结构的库。 plistParser 看起来很有用。
plistParser 示例:
输出:
This is a plist XML file. Apple uses them alot. I would recommend to find a library that parses the plist file into a proper native data structure. plistParser looks useful.
plistParser example:
Output:
虽然有点晚了,但我在我的项目中使用了这个确切的建议,并且我使用 plist 解析器将近一年了。最大的问题是,它是一种通用的 Plist 解析解决方案,非常适合配置和二进制文件,但如果不使用数百兆的 RAM,它就无法处理大型 iTunes 库。我最近为 php 创建了一个基于 SAX 的解析器,它将正确地流过 iTunes 文件,为每个歌曲条目提供正确格式化的数组。
包含您需要的 Github 要点的文章位于:
http://streeme.posterous.com/how-to-read-an-itunes-music-library-xml-file
It's a bit late, but I used this exact advice for my project and I was using plist parser for a nearly a year. The big problem is that it's kind of a general Plist parsing solution well adapted to config and binary files, but it doesn't handle large iTunes libraries without using hundreds of megabytes of RAM. I've recently created a SAX based parser for php that will properly stream through an iTunes file delivering a properly formatted array on each song entry.
the article including the Github gists you'll need is here:
http://streeme.posterous.com/how-to-read-an-itunes-music-library-xml-file