2

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

1 に答える 1

4

NSXMLParserXMLを解析するために利用します。次に、以下のメソッドを使用して、タグから属性値を取得します

- (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パーサーを使用しておらず、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);
于 2013-02-22T05:27:18.320 に答える