利用NSXMLParser
来解析 XML。然后使用下面的方法从标签中获取属性值
- (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"]);
}
如果您没有使用任何 XML Parser 并将您的 XML 视为简单字符串,那么请尝试像这样操作字符串
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);