简体   繁体   中英

How do i parser html code! I want get tag in html code for iPhone

NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";

Now , I want to get pic.jpg ,

How can I search the string?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM