'SDK를'에 해당되는 글 1건

  1. 2010.06.24 iPhone SDK를 사용해서 xml 파싱하는 방법
아이폰어플개발정보2010. 6. 24. 11:39
iPhone SDK를 사용해서 xml 파싱하는 방법

by alex 4. 3월 2009 07:50


//

//  XMLReader.h

 
#import

 
@class MSearchItem;

 
@interface XMLReader : NSObject {

@private

 MSearchItem *searchItemObj;

 NSMutableString *_contentOfCurrentUserSession;

 NSMutableArray *searchList;

}

 
@property (nonatomic, retain) MSearchItem *searchItemObj;

@property (nonatomic, retain) NSMutableString *contentOfCurrentUserSession;

@property (nonatomic, retain) NSMutableArray *searchList;

 
-(void)parseXMLFileAtURL:(NSURL *)URL parseXMLFile:(NSData *)fileData parseError:(NSError **)error;

 
@end

 
 
//

//  XMLReader.m

 
#import "XMLReader.h"

#import "MSearchItem.h"

 
 
@implementation XMLReader

 
@synthesize searchItemObj;

@synthesize contentOfCurrentUserSession = _contentOfCurrentUserSession;

@synthesize searchList;

 
-(void)parseXMLFileAtURL:(NSURL *)URL parseXMLFile:(NSData *)fileData parseError:(NSError **)error

{

 NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];

 //set self as deletegate of the parser so that it will receive the parser deletegate methods callback.

 [parser setDelegate:self];

 // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.

 [parser setShouldReportNamespacePrefixes:NO];

 [parser setShouldResolveExternalEntities:NO];

 
 [parser parse];

 
 NSError *parserError = [parser parserError];

 if(parserError && error)

 {

  *error = parserError;

 }

 [parser release];

}

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

 attributes:(NSDictionary *)attributeDict

{

    if (qName) {

        elementName = qName;

    }

    if ([elementName isEqualToString:@"searchList"] ) {

  searchList = [[NSMutableArray alloc] init];

 }   

 
    if ([elementName isEqualToString:@"searchItem"] ) {

        self.searchItemObj = [[MSearchItem alloc] init];

     return;

    }

 
    self.contentOfCurrentUserSession = [[NSMutableString alloc] init];

}

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

{    

    if (qName) {

        elementName = qName;

    }

 
    if ([elementName isEqualToString:@"searchId"]) {

        self.searchItemObj.searchId = [self.contentOfCurrentUserSession intValue];

        [self.contentOfCurrentUserSession release];

    }

   
    if ([elementName isEqualToString:@"engine_name"]) {

        self.searchItemObj.engine_name = self.contentOfCurrentUserSession;

        [self.contentOfCurrentUserSession release];

    }

 
 if ([elementName isEqualToString:@"icon_url"]) {

        self.searchItemObj.icon_url = self.contentOfCurrentUserSession;

        [self.contentOfCurrentUserSession release];

    }

 if ([elementName isEqualToString:@"search_string_prefix"]) {

        self.searchItemObj.search_string_prefix = self.contentOfCurrentUserSession;

  //NSLog(self.searchItemObj.search_string_prefix);

        [self.contentOfCurrentUserSession release];

    }

 if ([elementName isEqualToString:@"search_delimiter"]) {

        self.searchItemObj.search_delimiter = self.contentOfCurrentUserSession;

        [self.contentOfCurrentUserSession release];

    }

 if ([elementName isEqualToString:@"search_string_suffix"]) {

        self.searchItemObj.search_string_suffix = self.contentOfCurrentUserSession;

        [self.contentOfCurrentUserSession release];

    }

 
 if ([elementName isEqualToString:@"searchItem"] ) {

  [searchList addObject:searchItemObj];

 }

  
}

 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

 if(string){

  NSString *firstFour = [string substringToIndex:1];

  if([firstFour isEqualToString:@"\n"]){

   return;

  }

  
  [self.contentOfCurrentUserSession appendString:string];

 }

}

 
- (void)dealloc {

 [_contentOfCurrentUserSession release];

 [super dealloc];

}

 
@end



출처 : http://blog.naver.com/PostView.nhn?blogId=amoros21&logNo=140107111353
Posted by 오늘마감

댓글을 달아 주세요