我正在使用nsxmlparser解析rss,我想从rss中获取一些图像...我该怎么做?
所以这里是一个rss的例子
<p><img scr = "IMGURL"></p>
我怎样才能获得“IMGURL”
感谢, TC
答案 0 :(得分:2)
您需要使用像XQuery这样的xml解析器。查询将是:/ p / img @src 您可以使用以下功能:
NSString *stringForXQuery(NSXMLNode *node, NSString *query)
{
NSArray *results = [node objectsForXQuery:query constants:nil error:nil];
NSUInteger howManyResults = [results count];
if (howManyResults != 1) {
return nil;
}
return [[results objectAtIndex:0] stringValue];
}
并称之为:
NSString *imgURL = stringForXQuery([yourxmldocument rootElement], @"/p/img@src");
答案 1 :(得分:2)
这是一个快速入侵 - 只有当你知道字符串不会改变位置时才这样做。否则你冒着崩溃的风险。
//for demo purposes lets say your <p><img scr = "IMGURL"></p> is a string named sourceString
//chop it up into an array like this and grab it from the arrays position
//create an array
NSArray *tempArray = [sourceString componentsSeparatedByString:@"\""];
//what this has done, it has create an array of objects from your source string separated by "
//so in your tempArray you will have two objects
// objectAtIndex 0 will be: <p><img scr = you wont need this so we can ignore it
// objectAtIndex 1 will be the string you want. it will be: IMGURL
// so now you can quickly create a string from it like this
NSString * imgURL = [[tempArray objectAtIndex:1]description];
这是一个快速而肮脏的伎俩......但它有效!只要数据保持相同的格式。你的电话!