2011-07-29 6 views
0

NSDictionary의 NSArray에 대한 XML 구문 분석에 문제가 있습니다. 왜 그런지 모르겠지만 배열 대신 2 개의 객체 (이 경우)를 얻으면 같은 데이터를 가진 2 개의 객체를 가져옵니다 ... 왜죠?NSDictionary의 NSArray에 XML을 구문 분석 - 오류 - 왜?

@interface RLparseXMLToArrayOfDictionarys : NSObject <NSXMLParserDelegate> { 
    NSMutableArray *arrayWithResult; 
    NSMutableDictionary *tempDict; 
    NSMutableString *currentString; 
    NSString *groupKey; 
} 

@property (nonatomic, strong) NSString *groupKey; 
@property (nonatomic, retain) NSMutableArray *arrayWithResult; 
@property (nonatomic, retain) NSMutableDictionary *tempDict; 

-(NSArray *)parseXMLWithStringToArray:(NSString *)stringWithXML withGroupKey:(NSString *)groupKeyToIgnore; 

@end 




@implementation RLparseXMLToArrayOfDictionarys 

@synthesize groupKey; 
@synthesize arrayWithResult; 
@synthesize tempDict; 


- (id)init 
{ 
    self = [super init]; 
    if (self) { 

    } 

    return self; 
} 


-(NSArray *)parseXMLWithStringToArray:(NSString *)stringWithXML withGroupKey:(NSString *)groupKeyToIgnore{ 
    NSData *currentStringData = [stringWithXML dataUsingEncoding:NSUTF8StringEncoding]; 
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:currentStringData]; 
    [parser setDelegate:self]; 

    // Set Parser Options 
    [parser setShouldProcessNamespaces:NO]; 
    [parser setShouldReportNamespacePrefixes:NO]; 
    [parser setShouldResolveExternalEntities:NO]; 

    //key to ignore 
    self.groupKey = groupKeyToIgnore; 

    if (!arrayWithResult) { 
     arrayWithResult = [[NSMutableArray alloc] init]; 
    } 

    if (!tempDict) { 
     tempDict = [[NSMutableDictionary alloc] init]; 
    } 

    [parser parse]; 

    NSLog(@"return: %@", arrayWithResult); 

    return arrayWithResult; 
} 


#pragma mark - 
#pragma mark XML methods 

- (void)parserDidStartDocument:(NSXMLParser *)parser 
{ 

} 


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 

} 


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    if(!currentString){ 
     currentString = [[NSMutableString alloc] init]; 
    } 

    [currentString appendString:string]; 
} 


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    NSString *currentStringNoWhiteSpace = [currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    if ([elementName isEqualToString:groupKey]){ 
     [arrayWithResult addObject:tempDict]; 
//  [tempDict removeObjectsForKeys:[tempDict allKeys]]; 
    } 

    else if (currentStringNoWhiteSpace != nil) 
     [tempDict setValue:currentStringNoWhiteSpace forKey:elementName]; 

    currentStringNoWhiteSpace = nil; 
    currentString = nil; 
} 


- (void)parserDidEndDocument:(NSXMLParser *)parser { 

} 

@end 

답변

0

가 완료 :

여기에 코드입니다!

[tempDict removeObjectsForKeys:[tempDict allKeys]]; 로 교체 tempDict = [[NSMutableDictionary alloc] init];

: