-(void)GetAllWeeks
{
className=@"Week";
items=[[NSMutableArray alloc]init];
NSString * path1 = NSLocalizedString(@"appURL",nil);
path1 = [NSString stringWithFormat:@"%@Week.xml",path1];
[self parseXMLFileAtURL:path1];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(@"found file and started parsing");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
@try
{
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
[rssParser parse];
[pool drain];
[self setDataToDelegate:@""];
}
@catch (NSException *e) {
[self setDataToDelegate:@"error"];
}
}
-(void)setDataToDelegate:(NSString *)message
{
NSLog(@"%@", message);
}
--------------------------------------------------------------------------------
PARSING STARTED
--------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSString * errorString = [NSString stringWithFormat:@"Please ensure that this device is connected to the Internet."];
[self setDataToDelegate:errorString];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Start element: %@", elementName);
@try
{
if(![elementName isEqualToString:rootName])
{
if([elementName isEqualToString:className]) {
// create an instance of a class on run-time
objTeam = [[NSClassFromString(className) alloc] init];
}
else{
currentNodeName = [elementName copy];
currentNodeContent = [[NSMutableString alloc] init];
}
}
}
@catch (NSException *e) {
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"ended element: %@", elementName);
@try
{
if([elementName isEqualToString:className]) {
//Teams *obj_team = (Teams *)objTeam;
[items addObject:objTeam];
[objTeam release];
objTeam = nil;
}
else
{
if([elementName isEqualToString:currentNodeName]) {
// use key-value coding
[objTeam setValue:currentNodeContent forKey:elementName];
[currentNodeContent release];
currentNodeContent = nil;
[currentNodeName release];
currentNodeName = nil;
}
}
}
@catch (NSException *e) {
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[currentNodeContent appendString:string];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"all done!");
NSLog(@"items array has %d items", [items count]);
}
{
className=@"Week";
items=[[NSMutableArray alloc]init];
NSString * path1 = NSLocalizedString(@"appURL",nil);
path1 = [NSString stringWithFormat:@"%@Week.xml",path1];
[self parseXMLFileAtURL:path1];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(@"found file and started parsing");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
@try
{
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];
[rssParser parse];
[pool drain];
[self setDataToDelegate:@""];
}
@catch (NSException *e) {
[self setDataToDelegate:@"error"];
}
}
-(void)setDataToDelegate:(NSString *)message
{
NSLog(@"%@", message);
}
--------------------------------------------------------------------------------
PARSING STARTED
--------------------------------------------------------------------------------
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSString * errorString = [NSString stringWithFormat:@"Please ensure that this device is connected to the Internet."];
[self setDataToDelegate:errorString];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
NSLog(@"Start element: %@", elementName);
@try
{
if(![elementName isEqualToString:rootName])
{
if([elementName isEqualToString:className]) {
// create an instance of a class on run-time
objTeam = [[NSClassFromString(className) alloc] init];
}
else{
currentNodeName = [elementName copy];
currentNodeContent = [[NSMutableString alloc] init];
}
}
}
@catch (NSException *e) {
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"ended element: %@", elementName);
@try
{
if([elementName isEqualToString:className]) {
//Teams *obj_team = (Teams *)objTeam;
[items addObject:objTeam];
[objTeam release];
objTeam = nil;
}
else
{
if([elementName isEqualToString:currentNodeName]) {
// use key-value coding
[objTeam setValue:currentNodeContent forKey:elementName];
[currentNodeContent release];
currentNodeContent = nil;
[currentNodeName release];
currentNodeName = nil;
}
}
}
@catch (NSException *e) {
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[currentNodeContent appendString:string];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"all done!");
NSLog(@"items array has %d items", [items count]);
}
No comments:
Post a Comment