• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
MAX parsing (Re: TBXML question)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

MAX parsing (Re: TBXML question)


  • Subject: MAX parsing (Re: TBXML question)
  • From: Marcel Weiher <email@hidden>
  • Date: Sat, 14 Jan 2012 12:31:41 +0100

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 (email@hidden)

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:

This email sent to email@hidden

References: 
 >TBXML question (From: "Eric E. Dolecki" <email@hidden>)

  • Prev by Date: Re: Is slowing down bindings updates possible?
  • Next by Date: Re: Adding an observer to an NSOperation crashes my app.
  • Previous by thread: Re: TBXML question
  • Next by thread: Child windows appear to move when entering full-screen mode / how to detect full-screen transition
  • Index(es):
    • Date
    • Thread