2009-05-07 16 views
0

"title"と "noType"という2つの要素のXMLファイルを解析しています。これらを解析したら、NSString変数を含む独自のMasterクラスのインスタンスであるaMasterというオブジェクトに追加します。NSMutableArrayにオブジェクトを追加すると、XMLファイルから解析するときに順序が奇妙になる

次に、これらのインスタンスをシングルトンのNSMutableArrayに追加して、プログラム内の別の場所で呼び出します。問題は、私がそれらを呼び出すと、同じNSMutableArrayインデックス上にあるようには見えないということです。それぞれのインデックスにはタイトルまたはNOType要素が含まれています。間違っている?以下はパーサのコードです。本当にありがとう!!

#import "XMLParser.h" 
#import "Values.h" 
#import "Listing.h" 
#import "Master.h" 

@implementation XMLParser 

@synthesize sharedSingleton, aMaster; 

- (XMLParser *) initXMLParser { 

    [super init]; 
    sharedSingleton = [Values sharedValues]; 
    aMaster = [[Master init] alloc]; 
    return self; 
} 

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

    aMaster = [[Master alloc] init]; 

     //Extract the attribute here. 

    if ([elementName isEqualToString:@"intro"]) { 
     aMaster.intro = [attributeDict objectForKey:@"enabled"]; 
    } else if ([elementName isEqualToString:@"item"]) { 
     aMaster.item_type = [attributeDict objectForKey:@"type"]; 
     //NSLog(@"Did find item with type %@", [attributeDict objectForKey:@"type"]); 

     //NSLog(@"Reading id value :%@", aMaster.item_type); 
    } else { 
     //NSLog(@"No known elements"); 
    } 

    //NSLog(@"Processing Element: %@", elementName); //HERE 
} 

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

    if(!currentElementValue) 
     currentElementValue = [[NSMutableString alloc] initWithString:string]; 
    else { 
     [currentElementValue appendString:string];//[tempString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; 
     CFStringTrimWhitespace((CFMutableStringRef)currentElementValue); 
    } 
} 

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

    if ([elementName isEqualToString:@"item"]) { 
     [sharedSingleton.master addObject:aMaster]; 
     NSLog(@"Added %@ and %@ to the shared singleton", aMaster.title, aMaster.noType); //Only having one at a time added... don't know why 
     [aMaster release]; 

     aMaster = nil; 

    } else if ([elementName isEqualToString:@"title"]) { 
     [aMaster setValue:currentElementValue forKey:@"title"]; 
    } else if ([elementName isEqualToString:@"noType"]) { 
     [aMaster setValue:currentElementValue forKey:@"noType"]; 
     //NSLog(@"%@ should load into the singleton", aMaster.noType); 
    } 
    NSLog(@"delimiter"); 
    NSLog(@"%@ should load into the singleton", aMaster.title); 
    NSLog(@"%@ should load into the singleton", aMaster.noType); 

    [currentElementValue release]; 
    currentElementValue = nil; 

} 

- (void) dealloc { 
    [aMaster release]; 
    [currentElementValue release]; 
    [super dealloc]; 
} 

@end 

答えて

0

didStartElementが呼び出されるたびに、あなたはMasterクラスの新しいインスタンスにaMasterを設定しています。 didEndElementの実装に基づいて、新しいitemタグが見つかるたびに新しいインスタンスを作成する必要があるようです。これは、新しいインスタンスが各値に対して作成されるため、配列内の各エントリが1つの値または別の値を持つ理由になる可能性があります。

+0

ありがとう、アンディ!それが原因でした。私は完全にそれを逃していた。 –

+1

あなたの質問に答えるなら、あなたは答えを受け入れるべきです(私の答えの左側にあるチェックマークを押してください)。 – Andy

関連する問題