iTunes Music Library parsing help
iTunes Music Library parsing help
- Subject: iTunes Music Library parsing help
- From: Dave DeLong <email@hidden>
- Date: Sun, 06 Mar 2005 14:00:09 -0700
Hi everyone,
I've got some code that parses the iTunes Music Library.xml file by pushing it into a dictionary:
NSURL * iTunesDatabaseLocation = [NSURL fileURLWithPath:[@"~/music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath]];
NSDictionary * iTunesDatabase = [NSDictionary dictionaryWithContentsOfURL:iTunesDatabaseLocation];
I then iterate through the dictionary with an NSEnumerator to construct some custom Playlist objects that contain the list of all the songs in each playlist, etc.
The problem is that it takes a VERY long time to do this -- well over a minute. I think that's a little odd, because I wrote the exact same method in another language and it could pull all available track information, store each track in a seperate track object, then create the entire and correct playlist hierarchy with references to each track. It could do all of this in less than three seconds.
Perhaps I'm doing something wrong? Why on earth should it be taking more than 20 times as much time? Here's all the parsing code:
- (void)awakeFromNib
{
NSLog(@"Start");
songs = [[NSMutableArray alloc]init];
playlists = [[NSMutableArray alloc]init];
}
- (id)sender
{
NSURL * iTunesDatabaseLocation = [NSURL fileURLWithPath:[@"~/music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath]];
NSDictionary * iTunesDatabase = [NSDictionary dictionaryWithContentsOfURL:iTunesDatabaseLocation];
NSDictionary * tracks = [iTunesDatabase objectForKey:@"Tracks"];
NSEnumerator * enumerator = [tracks objectEnumerator];
NSDictionary * currentTrack;
while (currentTrack = [enumerator nextObject])
[songs addObject:currentTrack];
NSDictionary * lists = [iTunesDatabase objectForKey:@"Playlists"];
NSEnumerator * listEnumerator = [lists objectEnumerator];
NSDictionary * currentList;
while (currentList = [listEnumerator nextObject])
{
Playlist * aList = [[Playlist alloc] init];
[aList setName:[currentList objectForKey:@"Name"]];
NSArray * ids = [currentList objectForKey:@"Playlist Items"];
NSEnumerator * idEnumerator = [ids objectEnumerator];
NSDictionary * currentId;
NSMutableArray * playSongs = [[NSMutableArray alloc] init];
while (currentId = [idEnumerator nextObject])
{
int i = [[currentId objectForKey:@"Track ID"]intValue];
[playSongs addObject:[self songForID:i]];
}
[aList setSongs:playSongs];
[playlists addObject:aList];
}
NSEnumerator * playlistEnum = [playlists objectEnumerator];
Playlist * currList;
while (currList = [playlistEnum nextObject])
{
// NSLog([[currList name]stringByAppendingString:@":"]);
NSEnumerator * songEnum = [[currList songs]objectEnumerator];
NSDictionary * currSong;
while (currSong = [songEnum nextObject])
{
/* NSLog([@" "stringByAppendingString:[currSong objectForKey:@"Name"]]);
NSLog([@" Artist: "stringByAppendingString:[currSong objectForKey:@"Artist"]]);
NSLog([@" Album: "stringByAppendingString:[currSong objectForKey:@"Album"]]);
NSLog([@" Genre: "stringByAppendingString:[currSong objectForKey:@"Genre"]]);
NSLog([@" Kind: "stringByAppendingString:[currSong objectForKey:@"Kind"]]);
NSLog([@" Size: "stringByAppendingString:[[currSong objectForKey:@"Size"]stringValue]]);
*/
}
}
NSLog(@"Finish");
}
- (NSDictionary *)songForID:(int)ID
{
NSEnumerator * enumerator = [songs objectEnumerator];
NSDictionary * currentSong;
while (currentSong = [enumerator nextObject])
{
if ([[currentSong objectForKey:@"Track ID"]intValue] == ID )
return currentSong;
}
}
Any ideas?
Thanks a bunch!
Dave DeLong
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden