iOS - 检索所有 的最快方法在 html 字符串中?

发布于 2024-12-12 12:54:06 字数 185 浏览 2 评论 0原文

我有很多 HTML 字符串(来自 google reader 的新闻项目)需要处理。我主要需要做的是从 HTML 中检索所有 img 标签。

谁能告诉我最有效的方法?

谢谢

另外,如果我需要检索所有标签以及

标签怎么办?有什么最快的方法可以在一次运行中检索两个甚至多个标签?

谢谢

I have many HTML strings (news items from google reader) to process. Majorly what I need to do is to retrieve all img tags from the HTMLs.

Can anyone tell me a most efficient way to do that?

Thanks

Also, what if I need to retrieve all tags as well as

tags? Any fastest way to retrieve both or even more tags in one run?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-12-19 12:54:06

假设 HTML 字符串实际上是有效的 XML(即它们实际上是 XHTML),那么您应该考虑使用 XPath 来匹配名称为“IMG”的所有元素。我已经在 iOS 应用程序中使用了各种 C 库来成功地完成此类操作。

Assuming that the HTML strings are actually valid XML (i.e. they are actually XHTML), then you should consider using XPath to match all elements of the name "IMG". I've used various C libraries in an iOS application to do this kind of things successfully.

筱果果 2024-12-19 12:54:06

另一种尝试方法是使用 NSScanner 实例。假设您的 HTML 字符串位于名为 htmlString 的 NSString 中,您可以尝试如下操作:

NSScanner *scanner = [NSScanner scannerWithString:htmlString];
while ([scanner scanUpToString:@"<img" intoString:NULL]) {
    NSString *tagContents;
    if ([scanner scanUpToString:@">" intoString &tagContents]) {
        // Do something with tag contents
    }
    else {
        // Do nothing? I think this would be hit on the last time through the loop
    }
}

Another approach to try would be to use an NSScanner instances. Assuming you have your HTML string in an NSString called htmlString, you could try something like this:

NSScanner *scanner = [NSScanner scannerWithString:htmlString];
while ([scanner scanUpToString:@"<img" intoString:NULL]) {
    NSString *tagContents;
    if ([scanner scanUpToString:@">" intoString &tagContents]) {
        // Do something with tag contents
    }
    else {
        // Do nothing? I think this would be hit on the last time through the loop
    }
}
青衫儰鉨ミ守葔 2024-12-19 12:54:06

尝试 libtidy + NSXMLParser:

 doc = [[NSXMLDocument alloc] 
           initWithContentsOfURL:url
                         options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA)
                           error:&err];
 if (!doc) {
     doc = [[NSXMLDocument alloc] 
           initWithContentsOfURL:url
                         options:NSXMLDocumentTidyHTML
                           error:&err];
 }

从文档中,NSXMLDocumentTidyHTML在处理文档期间将 HTML 格式化为有效的 XHTML。

如果这不起作用,您可以尝试加载 HTML 源进入 UIWebView 并使用 javascript 访问 DOM。

Try libtidy + NSXMLParser:

 doc = [[NSXMLDocument alloc] 
           initWithContentsOfURL:url
                         options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA)
                           error:&err];
 if (!doc) {
     doc = [[NSXMLDocument alloc] 
           initWithContentsOfURL:url
                         options:NSXMLDocumentTidyHTML
                           error:&err];
 }

From the doc, NSXMLDocumentTidyHTML: Formats HTML into valid XHTML during processing of the document.

If this doesn't work, you can try loading the HTML source into an UIWebView and use javascript to access the DOM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文