0

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

次に、これらのインスタンスをシングルトンの 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
4

1 に答える 1

0

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

于 2009-05-07T18:36:11.017 に答える