【问题标题】:How do i parser html code! I want get tag in html code for iPhone我如何解析html代码!我想在 iPhone 的 html 代码中获取标签
【发布时间】:2011-08-02 03:37:10
【问题描述】:
NSString *str = @"<'html><'img src= 'pic.jpg'/><'/html>";

现在,我想得到pic.jpg

如何搜索字符串?

【问题讨论】:

    标签: html objective-c iphone tags


    【解决方案1】:

    代码依赖于libxml2 并且基本上是一个瘦包装器,它为使用Objective C 解析html 提供了一个更方便的接口。这仅在iOS 3.1.3 和3.2 上进行了测试,如果你使用的是OSX,你'重新调查使用 WebKit 来操纵你的 DOM 可能会更好。以下代码可能会对您有所帮助。

    //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 也可能有用。

    【讨论】:

    • 哇..这就是我想要的结果。谢谢你的帮助,HonestSuccess。你能帮我吗?如果我没有 libxml2 框架。我该怎么做才能得到结果?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2019-08-02
    • 1970-01-01
    相关资源
    最近更新 更多