Retrieve image from XML using NSXMLParser

247 views Asked by At

I have an RSS feed with the image of the feed encoded like this:

<imageItem src="http://www.example.com/picture.jpg">
<img width="386" height="173" src="http://www.abb-conversations.com/DACH/files/2015/06/Traumpraktikum-755-386x173.jpg" class="attachment-teaser-tablet wp-post-image" alt="Traumpraktikum-755" style="float:left; margin:0 15px 15px 0;"/>
</imageItem>

Using NSXMLParser, I was to be able to extract that src link. Currently the way I'm doing it does not return anything because it is looking for something like <imageItem> xxx.jpg </imageItem>.

Here is the code:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

  if ([self.element isEqualToString:@"title"]) {
    [self.title appendString:string];
  }
  else if ([self.element isEqualToString:@"link"]) {
    [self.link appendString:string];
  }
  else if ([self.element isEqualToString:@"imageItem"]) {

    [self.image appendString:string];

  }
}
2

There are 2 answers

0
Kishore Suthar On BEST ANSWER

Try this delegate method

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementName isEqual:@"img"])
        {
            NSLog(@"SRC: %@",[attributeDict objectForKey:@"src"]);
        }
    }
1
Midhun MP On

The image url is set as the attribute of that img element.

You need to implement the following NSXMLParser delegate

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{         
    if([elementName isEqualToString:@"img"])
    {
        NSString *urlValue = [attributeDict valueForKey:@"src"];
        NSLog(@"%@",urlValue);
    }
}

Refer NSXMLParserDelegate for more details