i need some help i want to load an xml file into a table view this is what i have so far, could anyone explain to me how to do this. I think it would involve bindings too which i know a bit about.开发者_开发知识库
NSString* libraryPath = @"~/Music/iTunes/iTunes Music Library.xml";
NSDictionary* musicLibrary = [ NSDictionary dictionaryWithContentsOfFile: libraryPath ];
Thanks, Sami.
You could use an NSXMLDocument
to parse the XML file, it might be a little easier to do than implement an NSXMLParser.
Here's an example (untested).
- (void) parseItunesLibrary {
NSError* error = nil;
NSString* libraryPath = nil;
// better way to get the iTunes database file (in case library was moved)
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSDictionary* iAppsDict = [defaults persistentDomainForName:@"com.apple.iApps"];
NSArray* itunesDBPath = [iAppsDict objectForKey:@"iTunesRecentDatabasePaths"];
if( [itunesDBPath count] > 0 ) {
libraryPath = [[itunesDBPath objectAtIndex: 0] stringByExpandingTildeInPath];
}
NSData* libraryData = [NSData dataWithContentsOfFile:libraryPath];
NSXMLDocument* libraryDoc = [[NSXMLDocument alloc] initWithData:libraryData options:NSXMLDocumentTidyXML error:&error];
// handle error before continuing here
if (error) {
NSLog(@"%@", [error localizedDescription]);
[libraryDoc release];
return;
}
NSXMLElement* root = [libraryDoc rootElement];
[libraryDoc release];
// assuming you wanted an array of the songs you can get them like this
NSArray* songsArray = [root nodesForXPath:@".//dict/dict/dict" error:nil];
// to access the different song elements you can do something like this
for(NSXMLElement* song in songsArray) {
NSString* songArtist = [[[song nodesForXPath:@"./Artist" error:nil] lastObject] stringValue];
}
}
Once you have this data, you can put it in an NSTableView
any way you like. Look at using a table datasource in the NSTableView Programming Guide.
精彩评论