我有一个 NSXML Parser 的实例,它解析一些 xml 并将其存储在一个对象数组中。XML 有一些图像 URL 和标题,我试图将它们添加到字典中,然后将字典添加到数据对象中的数组中。字典已正确设置,但不会添加到数组中。下面是xml和代码:
XML:
http://www.flashalert.net/rss.html?id=3753
代码:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSString *string1 = [string stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
NSString *string2 = [string1 stringByReplacingOccurrencesOfString:@""" withString:@"\""];
currentNodeContent = (NSMutableString *) [string2 stringByStrippingHTML];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementname isEqualToString:@"item"]) {
isStatus = YES;
currentPressRelease = [FCPressRelease alloc];
}
if ([elementname isEqualToString:@"media:content"]) {
isMedia = YES;
media = [[NSMutableDictionary alloc] init];
[media setValue:[attributeDict valueForKey:@"url"] forKey:@"url"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (isStatus)
{
if ([elementname isEqualToString:@"pubDate"])
{
currentPressRelease.pubDate = currentNodeContent;
}
if ([elementname isEqualToString:@"title"])
{
currentPressRelease.title = currentNodeContent;
}
if ([elementname isEqualToString:@"description"])
{
currentPressRelease.summary = currentNodeContent;
}
if ([elementname isEqualToString:@"guid"])
{
currentPressRelease.guid = currentNodeContent;
}
}
if (isMedia) {
if ([elementname isEqualToString:@"media:title"])
{
[media setValue:currentNodeContent forKey:@"title"];
}
}
if ([elementname isEqualToString:@"media:content"]) {
[currentPressRelease.images addObject:media];
media = nil;
}
if ([elementname isEqualToString:@"item"]) {
if (currentPressRelease != nil) {
[self.posts addObject:currentPressRelease];
currentPressRelease = nil;
currentNodeContent = nil;
}
else {
currentPressRelease = nil;
currentNodeContent = nil;
}
}
}
FCPressRelease.h
@interface FCPressRelease : NSObject
@property (strong, nonatomic) NSString *pubDate;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *summary;
@property (strong, nonatomic) NSString *guid;
@property (strong, nonatomic) NSMutableArray *images;
@end
FCPressRelease.m
@implementation FCPressRelease
@synthesize pubDate = _pubDate;
@synthesize title = _title;
@synthesize summary = _summary;
@synthesize guid = _guid;
@synthesize images = _images;
@end