NSXMLParser rss Image urls?

2020-06-28 03:49发布

I am parsing rss using nsxmlparser and I would like to get some images from the rss...how can I do that?

so here is an example of the rss

<p><img scr = "IMGURL"></p>

how can I get the "IMGURL"

thanks, TC

2条回答
We Are One
2楼-- · 2020-06-28 04:16

Here is a quick hack - only do this if you know the strings are not going to change position. Otherwise you risk crashing.

//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]; 

Its a quick and dirty trick... but it works! So long as the data stays the same format. Your call!

查看更多
淡お忘
3楼-- · 2020-06-28 04:19

You need to use a xml parser like XQuery. The query would be: /p/img@src You can use a function such as:

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];
}

And call it like this:

NSString *imgURL = stringForXQuery([yourxmldocument rootElement], @"/p/img@src");
查看更多
登录 后发表回答