开发者

Objective-C load iTunes Music Library.xml into NSTableView

I've read many guides about XML parsing, but was wondering if anyone could explain the different types of XML parsing and which one is best for performance, as I want to load the iTunes Music Library.XML into a table view, in a Mac app. Also was wondering if anyone could provide a tutorial on how to do this t开发者_运维技巧oo.


The easiest way to do this is to just use NSDictionary dictionaryWithContentsOfFile:

NSString* libraryPath = @"~/Music/iTunes/iTunes Music Library.xml";
NSDictionary* musicLibrary = [ NSDictionary dictionaryWithContentsOfFile: libraryPath ];

Also note that users can move their music library so a more complete example is:

NSString* libraryPath = @"~/Music/iTunes/iTunes Music Library.xml";
NSUserDefaults* prefs = [ NSUserDefaults standardUserDefaults ];
[ prefs synchronize ];
NSDictionary* iAppsPrefs = [ prefs persistentDomainForName: @"com.apple.iApps" ];
NSArray* recentPaths = [ iAppsPrefs objectForKey: @"iTunesRecentDatabasePaths" ];
if( [ recentPaths count ] > 0 ) {
    libraryPath = [ recentPaths objectAtIndex: 0 ];
}
NSDictionary* musicLibrary = [ NSDictionary dictionaryWithContentsOfFile: libraryPath ];

For really large libraries it can take several seconds to read the contents so it is best to do this on a thread.

Once you have this loaded then look at the structure of iTunes Music Library.xml in a text editor to see how you will have to walk the tree to format the information in a way that is appropriate to your needs.

EDIT:

With iTunes 11 Apple is now providing a framework on OSX to access the iTunes library. Documentation can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜