自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Objective-C解析HTML!

移動開發(fā) iOS
xml,json都有大量的庫來解析,我們?nèi)绾谓馕鰄tml呢? TFHpple是一個小型的封裝,可以用來解析html,它是對libxml的封裝,語法是xpath。 今天我看到一個直接用libxml來解析html。

使用Objective-C解析HTML或者XML,系統(tǒng)自帶有兩種方式一個是通過libxml,一個是通過NSXMLParser。不過這兩種方式都需要自己寫很多編碼來處理抓取下來的內(nèi)容,而且不是很直觀。
有一個比較好的類庫hpple,它是一個輕量級的包裝框架,可以很好的解決這個問題 。它是用XPath來定位和解析HTML或者XML。
安裝步驟:
-加入 libxml2 到你的項目中
   Menu Project->Edit Project Settings
   搜索 “Header Search Paths”
   添加新的 search path “${SDKROOT}/usr/include/libxml2″
   Enable recursive option
-加入 libxml2 library 到你的項目
   Menu Project->Edit Project Settings
   搜索 “Other Linker Flags”
   添加新的 search flag “-lxml2″
-將下面hpple的源代碼 加入到你的項目中:
HTFpple.h
HTFpple.m
HTFppleElement.h
HTFppleElement.m
XPathQuery.h
XPathQuery.m
-XPath學習地址http://www.w3schools.com/XPath/default.asp 
示例代碼:

  1. #import "TFHpple.h" 
  2. NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"]; 
  3. // Create parser 
  4. xpathParser = [[TFHpple alloc] initWithHTMLData:data]; 
  5. //Get all the cells of the 2nd row of the 3rd table  
  6. NSArray *elements  = [xpathParser search:@"//table[3]/tr[2]/td"]; 
  7. // Access the first cell 
  8. TFHppleElement *element = [elements objectAtIndex:0]; 
  9. // Get the text within the cell tag 
  10. NSString *content = [element content];   
  11. [xpathParser release]; 
  12. [data release]; 

下載 地址:https://github.com/topfunky/hpple 

另外,還有一個類似的解決方案可以參考
ElementParser http://github.com/Objective3/ElementParser

 

源碼下載:http://down.51cto.com/data/616107

 
責任編輯:閆佳明 來源: oschina
相關推薦

2011-08-10 18:07:29

Objective-C反射

2011-08-04 15:52:48

Objective-C HTML

2011-08-05 15:46:32

Objective-C 程序設計

2011-08-11 17:39:25

Objective-C筆試題

2011-08-15 14:02:36

Objective-C

2011-07-29 16:08:31

Objective-C 內(nèi)存

2013-06-20 10:40:32

Objective-C實現(xiàn)截圖

2011-05-11 11:20:26

Objective-C

2011-05-11 15:58:34

Objective-C

2011-08-02 13:16:36

Objective-C 語法 函數(shù)

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-05-11 13:54:08

Objective-C

2011-05-11 15:45:50

內(nèi)存管理Objective-C

2013-08-21 14:57:42

objective-c問題

2011-05-11 14:06:49

Objective-C

2011-08-04 11:15:46

Objective-C 構(gòu)造函數(shù) 構(gòu)造方法

2014-04-30 10:16:04

Objective-CiOS語法

2011-08-03 16:55:05

Objective-C 代理

2011-07-08 13:49:46

Objective-C UUID

2011-07-29 16:16:30

Objective-c block
點贊
收藏

51CTO技術棧公眾號