NSXMLParser 问题 - SeismicXML 示例

发布于 2024-10-21 08:52:57 字数 3864 浏览 3 评论 0原文

嘿, 我必须在我的 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。

我不知道为什么当解析器设置 .author 时 .titleText 例如会发生变化。

我已经对所有内容进行了至少 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;

Hey,
I have to parse XML in my iOS app. I took Apple's SeismicXML Sample as my base, but I'm experiencing a really strange behaviour.

These are my parser 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];
}

}

Everything works great, the XML Data comes correctly. The parser parses everything as it should.
The problem is in the parser didEndElement methode.

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

When I get "self.currentPhotoObject.titleText" via NSLog, I get the right parsed Data. But then:

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

}

When I get the NSLog of "self.currentPhotoObject.titleText" and from "self.currentPhotoObject.author" here, both give me the author.
In the third parsed methode it is the same. All three properties (titleText, author and locationText) are the locationText.

I have no idea why .titleText e.g. is changed when the parser sets .author.

I have doublechecked everything at least 10 times and compared it to the SeismicXML sample but I can't find the problem.
Please help me. I'm thankfull for every hint !

Greets Sebastian

ps: My properties in the .m file:

@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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-10-28 08:52:57

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

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

2)每次想要返回 NSMutableString 的值或将其分配给某些内容时都创建一个副本

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

It's because you assign same NSMutableString instance to all this properties.

1) Declare author, titleText, locationText properties as copy to avoid this in future.

2) Make a copy each time you want to return value of NSMutableString or assign it to something

self.currentPhotoObject.titleText = [[self.currentParsedCharacterData copy] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文