如何解析html代码!我想在iPhone的html代码中获取标签 NSString*str=@“

如何解析html代码!我想在iPhone的html代码中获取标签 NSString*str=@“,html,objective-c,iphone,tags,Html,Objective C,Iphone,Tags,该代码依赖于libxml2,基本上是一个薄包装器,为使用Objective C解析html提供了更方便的接口。这只在iOS 3.1.3和3.2上进行了测试,如果您使用OSX,您最好研究使用WebKit来操纵DOM。下面的代码可能会对您有所帮助 NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>"; //下载google源代码并打印所有图像的URL的示例 n错误*错误=nil; HTMLParser*

该代码依赖于
libxml2
,基本上是一个薄包装器,为使用Objective C解析html提供了更方便的接口。这只在iOS 3.1.3和3.2上进行了测试,如果您使用OSX,您最好研究使用WebKit来操纵DOM。下面的代码可能会对您有所帮助

NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";
//下载google源代码并打印所有图像的URL的示例
n错误*错误=nil;
HTMLParser*解析器=[[HTMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@]http://www.google.com“]错误:&错误];
如果(错误){
NSLog(@“错误:%@”,错误);
返回;
}
HTMLNode*bodyNode=[parser body]//找到body标签
NSArray*imageNodes=[bodyNode findChildTags:@“img”]//得到所有的

也可能有用。

wow。。这就是我想要的结果。谢谢你的帮助,真诚的成功。你能再帮我一次吗?如果我没有libxml2框架。我怎样做才能得到结果?
//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];