RSS Feed + NSXML Parser





// h ---------

#import <UIKit/UIKit.h>

typedef enum {
etNone = 0,
etItem
} eElementType;

@interface XMLSampleViewController : UITableViewController {
NSURLConnection *xmlConnection;
eElementType elementType;
NSMutableString *xmlValue;
NSMutableData *receiveData;
NSMutableArray *xmlParseData;
NSMutableDictionary *currectItem;
}


@end

// m------------------

#import "XMLSampleViewController.h"

@implementation XMLSampleViewController

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
//
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
//???: 안전해제
- (void)dealloc {
[xmlParseData release];
[xmlValue release];
[currectItem release];
    [super dealloc];
}
//???: 생성시, XML 연결, 파싱변수들 초기화
- (void)awakeFromNib {
xmlConnection = [[NSURLConnection alloc
initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://openapi.naver.com/search?key=dff214b1ec6366c19293c8af74f27536&query=isbn&display=10&start=1&target=book_adv&d_isbn=9788949151298"]]
delegate:self];
if (xmlConnection == nil)
NSLog(@"Connect error");
else
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
xmlParseData = [[NSMutableArray alloc] init];
xmlValue = [[NSMutableString alloc] init];
currectItem = [[NSMutableDictionary alloc] init];
receiveData = [[NSMutableData alloc] init];
}

#pragma mark URLConnection delegate methods
//???: 접속시, 로그출력
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Receive: %@, %@, %lld"
  [response URL],
  [response MIMEType],
  [response expectedContentLength]);
}
//???: 접속 실패시
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@", [error localizedDescription]);
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;  
}
//???: 데이타수신시, 버퍼에 추가
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receiveData appendData:data];
}
//???: 접속로딩 완료시, 파싱시작
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //파서 초기화
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:receiveData];
    [parser setDelegate:self];
    [parser parse];
    
    NSDictionary *dict = [xmlParseData objectAtIndex:0];
    NSLog([dict objectForKey:@"title"]);
    NSLog([dict objectForKey:@"author"]);
    NSLog([dict objectForKey:@"publisher"]);
    NSLog([dict objectForKey:@"pubdate"]);
    NSLog([dict objectForKey:@"image"]);    
[parser release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[xmlConnection release];
[receiveData release];
UITableView *tableView = (UITableView *)[self view];
[tableView reloadData];
}

#pragma mark XMLParse delegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"item"]) 
elementType = etItem;
[xmlValue setString:@""];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if (elementType != etItem)
return;
if ([elementName isEqualToString:@"title"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
} else if ([elementName isEqualToString:@"author"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
} else if ([elementName isEqualToString:@"publisher"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
} else if ([elementName isEqualToString:@"pubdate"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
} else if ([elementName isEqualToString:@"price"]) {
[currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
    } else if ([elementName isEqualToString:@"image"]) {
        [currectItem setValue:[NSString stringWithString:xmlValue] forKey:elementName];
    } else if ([elementName isEqualToString:@"item"]) {
[xmlParseData addObject:[NSDictionary dictionaryWithDictionary:currectItem]];
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (elementType == etItem) {
[xmlValue appendString:string];
}
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [xmlParseData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
NSDictionary *dict = [xmlParseData objectAtIndex:indexPath.row];
[[cell textLabel] setText:[dict objectForKey:@"title"]];
    return cell;
}

@end

댓글

이 블로그의 인기 게시물

파이썬으로 Homomorphic Filtering 하기

파이썬으로 2D FFT/iFFT 하기: numpy 버전