views:

331

answers:

4

Hi Guys..

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/'&gt;&lt;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'&gt; 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

+1  A: 

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.

Matthew Flaschen
Hey buddy..Thanks for your help...i will inform you whether your idea worked or any related doubtsThanks againArun
Arun
A: 

Have you thought about using regexp?

alkar
Hey buddy..Thanks for your help...i will inform you whether your idea worked or any related doubtsThanks againArun
Arun
A: 
NSString *str = @"<![CDATA[<a href='http://www.okmagazine.com/posts/view/13756/'&gt;&lt;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);
oxigen
Hey buddy..Thanks for your help...i will inform you whether your idea worked or any related doubtsThanks againArun
Arun
Hi BuddyYour idea worked..Thanks a tonRegardsArun
Arun
A: 

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
Roger Nolan
Hey buddy..Thanks for your help...i will inform you whether your idea worked or any related doubtsThanks againArun
Arun
I hope you do. For future reference, pasting replies isn't as useful as actually voting on answers and selecting a correct answer. Even better, post your solution if it is different to one of the answers suggested. SO isn't just about answering your questions, it's about building an authoratative database of questions and answers.
Roger Nolan