2012-02-20 82 views
0

我想知道是否有人对我的问题有任何想法。我需要从UIWebView加载的html文件中提取所有图像文件。我把文件加载到NSString中,现在需要解析文件。我已经通过用componentsSeparatedByString创建一个数组来搜索.jpg,.gif等。然后尝试向后工作以到达文件的开头。我最好的解决办法是能够解析出一个NSArray的html包含img src =“source”width =“”height =“”等等HTML图像字符串解析器

任何帮助或提示将不胜感激。我最后的努力是从整个文件的左侧到右侧进行搜索/替换,以找到我需要的字符串,但希望有更快的方法。

回答

0

不解析HTML,使用libxml2。它具有广泛的面向HTML的解析/遍历功能,可让您通过元素以编程方式导航文档。

我还没有得到面向HTML的示例代码,但它应该只是一个htmlReadDoc()的问题来获取解析的文档;然后调整你的遍历从read tree example

void print_element_names(xmlNode * a_node) 
{ 
    xmlNode *cur_node = NULL; 

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) { 
     if (cur_node->type == XML_ELEMENT_NODE) { 
      printf("node type: Element, name: %s\n", cur_node->name); 
     } 

     print_element_names(cur_node->children); 
    } 
} 

// ... call your version of this function with the root node of the document