Cocoa 通过网络获取xml文件并解析

通过一个url从网络获取一份xml文件,并解析其内容。

1、第一种方法是利用了NSData的一个构造函数,傻瓜化的通过网络获取文件,而不用考虑网络相关的各种细节。

NSURL *url = [NSURL URLWithString:@"http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction"]; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; NSData *xmlData = [NSData dataWithContentsOfURL:url]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; if (xmlData == nil) { NSLog(@"File read failed!:%@", xmlData); return; } NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData]; [parser setDelegate:self]; [parser parse]; [parser release]; 

2、第二种方法是通过NSUrlConnection建立网络链接,并读取返回的数据。

- (void)parseXML { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //获取xml self.xmlData = [NSMutableData data]; [[NSURLCache sharedURLCache] removeAllCachedResponses]; NSURL *url = [NSURL URLWithString:@"http://222.73.161.212/ispace2/servlet/com.lemon.xml.XmlAction"]; NSURLRequest *theRequest = [NSURLRequest requestWithURL:url]; //create the connection with the request and start loading the data NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; [self performSelectorOnMainThread:@selector(downloadStarted) withObject:nil waitUntilDone:NO]; if (urlConnection != nil) { do { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } while (!getXmlDone); } [urlConnection release]; #ifdef _DEBUG NSLog(@"item count:%d", [self.itemArray count]); for (int i=0; i<[self.itemArray count]; i++) { QuestionItem *item = [self.itemArray objectAtIndex:i]; NSLog(@"title:%@,reverse:%d", item.title, item.reverse); } #endif [pool release]; } #pragma mark NSURLConnection Delegate methods /* Disable caching so that each time we run this app we are starting with a clean slate. You may not want to do this in your application. */ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { return nil; } // Forward errors to the delegate. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { getXmlDone = YES; } // Called when a chunk of data has been downloaded. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the downloaded chunk of data. [self.xmlData appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self performSelectorOnMainThread:@selector(downloadEnded) withObject:nil waitUntilDone:NO]; //解析xml NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.xmlData]; parser.delegate = self; [parser parse]; [parser release]; [self performSelectorOnMainThread:@selector(changeView) withObject:nil waitUntilDone:NO]; self.xmlData = nil; getXmlDone = YES; } - (void)downloadStarted { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; } - (void)downloadEnded { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }

 

相比之下,第一种方法非常简单,第二种方法的灵活性更好,当然复杂度也大大增加。

你可能感兴趣的:(xml,网络,cocoa,url,each,caching)