Using NSXMLParser with CDATA
Asked Answered
D

2

12

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];
}

}

Dunker answered 8/7, 2009 at 1:55 Comment(0)
L
4

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.

Lovettalovich answered 8/7, 2009 at 2:4 Comment(3)
Yes, there is an image URL in there. How can I extract an image URL using NSXMLParser?Dunker
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.Lovettalovich
CMSes such as Wordpress have the description in CDATA tags. An Iphone coder will likely be asked to show the description text in the RSS reader part of his app, just like all RSS readers do. GabrielN's answer is the solution.Observation
S
21

You can use

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
Synge answered 16/4, 2010 at 14:1 Comment(3)
This works great. If you need to parse further, just create another parser.Worrisome
This will be the answer for most who search for this to get a job done. It's not at all obvious that initWithData will give a valid string from the found NSData, but of course when you see it you go 'doh!'. :) Worked for me, +1.Observation
@Synge Please see my question #43539807. How can I use your code hearGenisia
L
4

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.

Lovettalovich answered 8/7, 2009 at 2:4 Comment(3)
Yes, there is an image URL in there. How can I extract an image URL using NSXMLParser?Dunker
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.Lovettalovich
CMSes such as Wordpress have the description in CDATA tags. An Iphone coder will likely be asked to show the description text in the RSS reader part of his app, just like all RSS readers do. GabrielN's answer is the solution.Observation

© 2022 - 2024 — McMap. All rights reserved.