NSXMLParser问题 - SeismicXML示例

时间:2011-03-13 08:48:10

标签: iphone xml ios nsxmlparser

嘿 我必须在我的iOS应用程序中解析XML。我把Apple的SeismicXML Sample作为我的基础,但我遇到了一种非常奇怪的行为。

这些是我的解析器方法:

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

if ([elementName isEqualToString:kEntryElementName]) {
    Photo *photo = [[Photo alloc] init];
    self.currentPhotoObject = photo;
    [photo release];

} else if ([elementName isEqualToString:kTitleElementName] ||
       [elementName isEqualToString:kLocationElementName] ||
       [elementName isEqualToString:kAuthorElementName]) {

    accumulatingParsedCharacterData = YES;

    [currentParsedCharacterData setString:@""];
}

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
                                  namespaceURI:(NSString *)namespaceURI
                                 qualifiedName:(NSString *)qName {     
if ([elementName isEqualToString:kEntryElementName]) {
    NSLog(@"Did End - Titel:%@", self.currentPhotoObject.titleText);
    NSLog(@"Did End - Location:%@", self.currentPhotoObject.locationText);
    NSLog(@"Did End - Author:%@", self.currentPhotoObject.author);

    [self.currentParseBatch addObject:self.currentPhotoObject];

    parsedPhotosCounter++;
    if ([self.currentParseBatch count] >= kMaximumNumberOfPhotosToParse) {
        [self performSelectorOnMainThread:@selector(addPhotosToList:)
                               withObject:self.currentParseBatch
                            waitUntilDone:NO];
        self.currentParseBatch = [NSMutableArray array];
    }
}    

else if ([elementName isEqualToString:kTitleElementName]) {
    self.currentPhotoObject.titleText = self.currentParsedCharacterData;
} 

else if ([elementName isEqualToString:kAuthorElementName]) {
    self.currentPhotoObject.author = self.currentParsedCharacterData;

}

else if ([elementName isEqualToString:kLocationElementName]) {
    self.currentPhotoObject.locationText = self.currentParsedCharacterData;        
} 

accumulatingParsedCharacterData = NO;

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (accumulatingParsedCharacterData) {
    // If the current element is one whose content we care about, append 'string'
    // to the property that holds the content of the current element.
    //
    [self.currentParsedCharacterData appendString:string];
}

}

一切都很好,XML数据正确。解析器解析所有内容。 问题出在解析器didEndElement方法中。

 else if ([elementName isEqualToString:kTitleElementName]) {
    self.currentPhotoObject.titleText = self.currentParsedCharacterData;
} 

当我通过NSLog获得“self.currentPhotoObject.titleText”时,我得到了正确的解析数据。但那时:

else if ([elementName isEqualToString:kAuthorElementName]) {
    self.currentPhotoObject.author = self.currentParsedCharacterData;

}

当我从这里得到“self.currentPhotoObject.titleText”的NSLog和“self.currentPhotoObject.author”时,两者都给了我作者。 在第三个解析的方法中,它是相同的。所有三个属性(titleText,author和locationText)都是locationText。

我不知道为什么.titleText例如解析器设置.author。

时更改

我已将所有内容重复检查至少10次并将其与SeismicXML样本进行比较,但我找不到问题。 请帮我。我很感激每一个提示!

Greets Sebastian

ps:.m文件中的我的属性:

@interface ParseOperation () <NSXMLParserDelegate>
@property (nonatomic, retain) Photo *currentPhotoObject;
@property (nonatomic, retain) NSMutableArray *currentParseBatch;
@property (nonatomic, retain) NSMutableString *currentParsedCharacterData;
@end

@implementation ParseOperation

@synthesize photoData, currentPhotoObject, currentParsedCharacterData, currentParseBatch;

1 个答案:

答案 0 :(得分:1)

这是因为您为所有这些属性分配了相同的NSMutableString实例。

1)将authortitleTextlocationText属性声明为copy,以避免将来发生这种情况。

2)每次要返回NSMutableString的值或将其分配给某个内容时复制

self.currentPhotoObject.titleText = [[self.currentParsedCharacterData copy] autorelease];