我如何解析html代码!我想在iPhone的HTML代码中获取标签 [英] How do i parser html code! I want get tag in html code for iPhone
本文介绍了我如何解析html代码!我想在iPhone的HTML代码中获取标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
现在,我想获得 pic.jpg
,
如何搜索字符串?
How can I search the string?
推荐答案
代码依赖于 libxml2
,它基本上是一个简单的包装器,它为使用Objective C解析html提供了一个更方便的接口。这仅在iOS 3.1.3& 3.2,如果你使用的是OSX,你最好使用WebKit来操纵你的DOM。以下代码可能对您有所帮助。
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.
这篇关于我如何解析html代码!我想在iPhone的HTML代码中获取标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文