views:

1845

answers:

3

I'm parsing an RSS feed with NSXMLParser and it's working fine for the title and other strings but one of the elements is an image thats like

<!CDATA <a href="http:image..etc>

How do I add that as my cell image in my table view? Would I define that as an image type?

This is what i'm using to do my parsing:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{      
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
 // clear out our story item caches...
 item = [[NSMutableDictionary alloc] init];
 currentTitle = [[NSMutableString alloc] init];
 currentDate = [ [NSMutableString alloc] init];
 currentSummary = [[NSMutableString alloc] init];
 currentLink = [[NSMutableString alloc] init];
}

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
//NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"item"]) {
 // save values to an item, then store that item into the array...
 [item setObject:currentTitle forKey:@"title"];
 [item setObject:currentLink forKey:@"link"];
 [item setObject:currentSummary forKey:@"description"];
 [item setObject:currentDate forKey:@"date"];

 [stories addObject:[item copy]];
 NSLog(@"adding story: %@", currentTitle);
}

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"title"]) {
 [currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"link"]) {
 [currentLink appendString:string];
} else if ([currentElement isEqualToString:@"description"]) {
 [currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
 [currentDate appendString:string];
}

}

+4  A: 

The point of CDATA is that anything within it is not treated as part of the XML document. So if you have tags in CDATA the parser will ignore them.

I'm guessing this CDATA is in the description element. So you'll need to extract the tags from the "description" element, either manually or via another instance of a parser.

Justicle
Yes, there is an image URL in there. How can I extract an image URL using NSXMLParser?
Xcoder
Have a look at the string inside the CDATA section. If it is always valid xml, fire up another instance of NSXMLParser on *that* string.If it isn't valid XML, you'll have to do Something Else™, for example, find the tag manually.
Justicle
A: 

I am having the same EXACT ISSUE! If you ever figured it out, could you email me at [email protected] ???

Matthew Saeger
+1  A: 

You can use

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
GabrielN
This works great. If you need to parse further, just create another parser.
Mariano