我有一个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
答案 0 :(得分:0)
您应该使用init方法来完成FCPressRelease
currentPressRelease = [FCPressRelease alloc] init];