如何从iOS中xml文件的d <description>标签下的<img src=""/>标签中提取图像网址?</description>

时间:2013-02-22 05:20:29

标签: ios objective-c xml parsing

XML正文:

<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>

1 个答案:

答案 0 :(得分:4)

利用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视为简单的String,那么尝试操作字符串

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);