嘿, 我必須在我的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;
是currentParsedCharacterData定義爲@property每次製作副本嗎? – phi 2011-03-13 09:28:51
是的,它是@私有的.h。我將.m文件中的屬性代碼添加到了上面的問題中。 – 2011-03-13 09:41:19