Reputation: 75
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
Now , I want to get pic.jpg
,
How can I search the string?
Upvotes: 0
Views: 1008
Reputation: 2645
The code depends on libxml2
and is basically a thin wrapper that provides a more convenient interface for parsing html with Objective C. This has only been tested on iOS 3.1.3 & 3.2, if you're using OSX you're probably better off investigating using WebKit to manipulate your DOM. The following code might help you.
//Example to download google's source and print out the urls of all the images
NSError * error = nil;
HTMLParser * parser = [[HTMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] error:&error];
if (error) {
NSLog(@"Error: %@", error);
return;
}
HTMLNode * bodyNode = [parser body]; //Find the body tag
NSArray * imageNodes = [bodyNode findChildTags:@"img"]; //Get all the <img alt="" />
for (HTMLNode * imageNode in imageNodes) { //Loop through all the tags
NSLog(@"Found image with src: %@", [imageNode getAttributeNamed:@"src"]); //Echo the src=""
}
[parser release];
This libxml wrapper for Objective C may also be useful.
Upvotes: 2