Akhil
Akhil

Reputation: 170

how can i extract the image url from <img src> tag under d<description > tag of xml file in iOS?

XML Body:

<description>
<img src="http://static.ibnlive.in.com/ibnlive/pix/slideshow/02-2013/pics-serial-blasts/hyd-blasts-90.jpg" alt="In pics: Serial blasts in Hyderabad kill 14" title="In pics: Serial blasts in Hyderabad kill 14" border="0" width="90" height="62" align="left" hspace="5"/>At least 14 people are feared to have been killed in a series of explosions on Thursday evening which took place in Dilsukh Nagar bus stand area of Hyderabad.
</description>

Upvotes: 2

Views: 1889

Answers (1)

βhargavḯ
βhargavḯ

Reputation: 9836

Make use of NSXMLParser to parse XML. And then use below method to get attribute value from tag

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

You can extract attribute value like this.

if ([elementName isEqual:@"img"])
{
     NSLog(@"SRC: %@",[attributeDict objectForKey:@"src"]);
}

And if you are not using any XML Parser and treating your XML as simple String then try to manipulate string like

NSString *imgURL= @"<description><img src=\"http://static.ibnlive.in.com/ibnlive/pix/slideshow/02-2013/pics-serial-blasts/hyd-blasts-90.jpg\" alt=\"In pics: Serial blasts in Hyderabad kill 14\" title=\"In pics: Serial blasts in Hyderabad kill 14\" border=\"0\" width=\"90\" height=\"62\" align=\"left\" hspace=\"5\"/>At least 14 people are feared to have been killed in a series of explosions on Thursday evening which took place in Dilsukh Nagar bus stand area of Hyderabad.</description>";

imgURL = [imgURL substringFromIndex:[imgURL rangeOfString:@"src="].location+[@"src=" length]+1];
imgURL = [imgURL substringToIndex:[imgURL rangeOfString:@"alt="].location-2];
NSLog(@"%@",imgURL);

Upvotes: 4

Related Questions