2011-03-13 77 views
1

嘿, 我必须在我的iOS应用程序中解析XML。我将Apple的SeismicXML Sample作为我的基础,但我遇到了一个非常奇怪的行为。NSXMLParser问题 - SeismicXML示例

这些都是我的解析器methodes:

- (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时更改。

我已将doublechecked至少10次,并将其与SeismicXML示例进行比较,但找不到问题。 请帮帮我。我很感谢每一个提示!

迎接塞巴斯蒂安

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; 
+0

是currentParsedCharacterData定义为@property每次制作副本吗? – phi 2011-03-13 09:28:51

+0

是的,它是@私有的.h。我将.m文件中的属性代码添加到了上面的问题中。 – 2011-03-13 09:41:19

回答

1

这是因为分配相同NSMutableString实例,这一切属性。

1)声明author,titleText,locationText属性为copy以避免将来出现这种情况。

2)要返回的NSMutableString值或指定给一些

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

老兄,非常感谢你!你让我今天一整天都感觉很好 !!! – 2011-03-13 11:08:15