Reputation: 151
I am trying to parse an XML file in which an element named "description" is as given below:
<description>
<![CDATA[
<a href='http://www.okmagazine.com/posts/view/13756/'>
<img src='http://www.okmagazine.com/img/photos/thumbs/27044' />
</a>
<br />
Ashlee and Pete take their tiny tot to FAO Schwarz in NYC for some new toys.
<p> <strong>Pete Wentz</strong> and <strong>Ashlee Simpson Wentz</strong> made the new parent pilgrimage to New York’s FAO Schwarz today, where 6-month old <strong>Bronx Mowgli </strong>was the...]]>
</description>
What I want is to get the link in the tag <img src='http://www.okmagazine.com/img/photos/thumbs/27044'>
using which I can display an image in my image view... How can I separate this string from the contents of description tag?
A part of code when parsing is as given below
- (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];
}
}
Please help
regards
Arun
Upvotes: 2
Views: 585
Reputation: 17170
Attributes are passed in to the didStartElement delegate method of the parser as a dictionary of strings keyed by attribute name. Thus, you can extract the urls you want from the attributes using NSDictionary's objectForKey: with the attribute name as the key. i.e.:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if([elementName compare: @"img"] == NSOrderedSame) // check for <img ...> element
{
NSString* url = [attributeDictionary objectForKey:@"src"];
// url now contains the url you require from the HTML
Upvotes: 0
Reputation: 6263
NSString *str = @"<![CDATA[<a href='http://www.okmagazine.com/posts/view/13756/'><img src='http://www.okmagazine.com/img/photos/thumbs/27044' /></a><br />Ashlee and Pete take their tiny tot to FAO Schwarz in NYC for some new toys. <p> <strong>Pete Wentz</strong> and <strong>Ashlee Simpson Wentz</strong> made the new parent pilgrimage to New York’s FAO Schwarz today, where 6-month old <strong>Bronx Mowgli </strong>was the...]]>";
NSRange range = [str rangeOfString: @"<img src='"];
str = [str substringFromIndex: range.location + range.length];
range = [str rangeOfString: @"'"];
str =[str substringToIndex: range.location];
CFShow(str);
Upvotes: 0
Reputation: 284816
I've never used that exact framework, but what you have to keep in mind is that while it will notify you when it finds the CDATA, anything inside is just plain-text to the parser. So it looks like you want to implement foundCDATA. You'll get passed a NSData block, and from there you have to parse the contents. Now, you can use another parser to do that, but it's probably faster just to do manual substring.
Upvotes: 1