【发布时间】:2011-03-13 08:48:10
【问题描述】:
嘿, 我必须在我的 iOS 应用程序中解析 XML。我以 Apple 的 SeismicXML 样本为基础,但我遇到了一种非常奇怪的行为。
这些是我的解析器方法:
- (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”和“self.currentPhotoObject.author”的NSLog时,都给我作者。 在第三个解析方法中它是相同的。所有三个属性(titleText、author 和 locationText)都是 locationText。
我不知道为什么 .titleText 例如解析器设置 .author 时更改。
我已经仔细检查了所有内容至少 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;
【问题讨论】:
-
currentParsedCharacterData 是否定义为@property?
-
是的,它是.h 中的@private。我在上面的问题中添加了.m文件中属性的代码。
标签: iphone xml ios nsxmlparser