2012-02-20 70 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