On Jan 13, 2012, at 15:23 , Eric E. Dolecki wrote: > I have XML like this: > > <xml version="1.0" encoding="UTF-8"?> > <users> > <user name="Eric Dolecki"> > <playlist name="Iron Maiden" source="Spotify"/> > <playlist name="Kate Bush" source="Pandora"/> > </user> > </users>
Don't know much about TBXML, but with MAX, the following code parses the file (including model classes and scaffolding): ---------- snip ---- #import <MPWXmlKit/MPWMAXParser.h> @interface User:NSObject { NSString *name; NSArray *playlist; } -initWithName:(NSString*)newName playlist:(NSArray*)newPlaylist; @end @implementation User -initWithName:(NSString*)newName playlist:(NSArray*)newPlaylist { if ( (self=[super init]) ) { name=[newName retain]; playlist=[newPlaylist retain]; } return self; } -description { return [NSString stringWithFormat:@"User: '%@' with songs: %@",name,playlist]; } -(void)dealloc { [name release]; [playlist release]; [super dealloc]; } @end @interface Song : NSObject { NSString *name; NSString *source; } -initWithName:(NSString*)newName source:(NSString*)newSource; @end @implementation Song -initWithName:(NSString*)newName source:(NSString*)newSource { if ( (self=[super init]) ) { name=[newName retain]; source=[newSource retain]; } return self; } -description { return [NSString stringWithFormat:@"Song: '%@' with source: %@",name,source]; } -(void)dealloc { [name release]; [source release]; [super dealloc]; } @end @interface SongParser : NSObject { id users; } @end @implementation SongParser -usersElement:children attributes:attrs parser:parser { users= [[children allValues] retain]; return nil; } -userElement:children attributes:attributes parser:parser { return [[User alloc] initWithName:[attributes objectForKey:@"name"] playlist:[children allValues]]; } -playlistElement:children attributes:attributes parser:parser { return [[Song alloc] initWithName:[attributes objectForKey:@"name"] source:[attributes objectForKey:@"source"]]; } -parseSongs:(NSString*)path { NSData *xmldata=[NSData dataWithContentsOfMappedFile:path]; MPWMAXParser *parser=[MPWMAXParser parser]; [parser setHandler:self forElements:[NSArray arrayWithObjects:@"users",@"user",@"playlist",nil] inNamespace:nil prefix:@"" map:nil]; [parser parse:xmldata]; return users; } @end int main( int argc, char *argv[] ) { [NSAutoreleasePool new]; id result=[[[SongParser new] autorelease] parseSongs:[NSString stringWithUTF8String:argv[1]]]; NSLog(@"result: %@",result); return 0; } ----------------- snip --------------- marcel@localhost[XML]cc -Wall -o maxparse maxparse.m -framework MPWXmlKit -framework MPWFoundation -framework Foundation && ./maxparse tbxml.xml 2012-01-14 12:28:19.230 maxparse[33421:507] result: ( "User: 'Eric Dolecki' with songs: (\n \"Song: 'Iron Maiden' with source: Spotify\",\n \"Song: 'Kate Bush' with source: Pandora\"\n)" ) _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com