顯示具有 xml 標籤的文章。 顯示所有文章
顯示具有 xml 標籤的文章。 顯示所有文章

2012年8月17日 星期五

Nokogiri

想不到 太久沒寫 Ruby

當時好用的 HTML Parser 工具 hpricot 已經終止了

不過還好還有 Nokogiri

以前使用過 hpricot 的朋友

可以完全無痛移植 Nokogiri

 C:\DevKit>gem install nokogiri
Fetching: nokogiri-1.5.5-x86-mingw32.gem (100%)
Successfully installed nokogiri-1.5.5-x86-mingw32
1 gem installed
Installing ri documentation for nokogiri-1.5.5-x86-mingw32...
Installing RDoc documentation for nokogiri-1.5.5-x86-mingw32...

一樣快速好用歐 並且支援 xpath, css, tag屬性 搜尋




require 'nokogiri'
require 'open-uri'

# Get a Nokogiri::HTML:Document for the page we’re interested in...

doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))

# Do funky things with it using Nokogiri::XML::Node methods...

####
# Search for nodes by css
doc.css('h3.r a').each do |link|
puts link.content
end

####
# Search for nodes by xpath
doc.xpath('//h3/a').each do |link|
puts link.content
end

####
# Or mix and match.
doc.search('h3.r a.l', '//h3/a').each do |link|
puts link.content
end




2012年8月10日 星期五

如何載入預設資料,使用 Core Data

Core Data on iOS 5 Tutorial: How To Preload and Import Existing Data

裡面提到兩種方式

  1. 執行App時,從其他資料匯入到資料戶,例如 JSON, XML, PLIST。
  2. 提供已經載入資料的資料庫。

在這裡我個人是採用第二種方式,

原因是,在邊寫程式邊調整資料庫的結構時,

最後也把資料庫弄好,並且整理好了,

所以在 App 第一次啟動時,

在 Core Data 初始化之前,需要先判斷 .sqlite 檔是否已經存在,

(Core Data 是 基於 SQLite 技術的物件式關連資料庫)

不存在,把之前準備好的資料庫檔 .sqlite (放在 App 的資料夾裡,跟 .h .m 放一起)

複製到 App 的 Document 目錄即可。


沒有採用第一種方式的原因是,

需要判斷是否已經載入之外,

還得另外寫匯入程式,

因為懶惰的關係,所以採用第二種方式唷!