Ruby的XML格式數(shù)據(jù)解析庫Nokogiri的使用進(jìn)階
一、基礎(chǔ)語法
1.直接以字符串形式獲取nokogiri對(duì)象:
html_doc = Nokogiri::HTML("<html><body><h1>Mr. Belvedere Fan Club</h1></body></html>")
xml_doc = Nokogiri::XML("<root><aliens><alien><name>Alf</name></alien></aliens></root>")
這里的html_doc和xml_doc就是nokogiri文件
2.也可以通過文件句柄獲取nokogiri對(duì)象:
f = File.open("blossom.xml")
doc = Nokogiri::XML(f)
f.close
3.還可以直接從網(wǎng)站獲取:
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.xxx.com/"))
二、XML文件解析實(shí)例
從XML/HTML文件里抓取字段的常用方法:
現(xiàn)在有一個(gè)名為shows.xml的文件,內(nèi)容如下:
<root>
<sitcoms>
<sitcom>
<name>Married with Children</name>
<characters>
<character>Al Bundy</character>
<character>Bud Bundy</character>
<character>Marcy Darcy</character>
</characters>
</sitcom>
<sitcom>
<name>Perfect Strangers</name>
<characters>
<character>Larry Appleton</character>
<character>Balki Bartokomous</character>
</characters>
</sitcom>
</sitcoms>
<dramas>
<drama>
<name>The A-Team</name>
<characters>
<character>John "Hannibal" Smith</character>
<character>Templeton "Face" Peck</character>
<character>"B.A." Baracus</character>
<character>"Howling Mad" Murdock</character>
</characters>
</drama>
</dramas>
</root>
如果想把所有character標(biāo)簽的內(nèi)容查找出來,可以這樣處理:
@doc = Nokogiri::XML(File.open("shows.xml"))
@doc.xpath("http://character")
xpath和css方法,返回的是一個(gè)結(jié)點(diǎn)列表,類似于一個(gè)數(shù)組,它的內(nèi)容就是從文件中查找出來的符合匹配規(guī)則的結(jié)點(diǎn).
把dramas結(jié)點(diǎn)里的character結(jié)點(diǎn)列表查出來:
@doc.xpath("http://dramas//character")
更有可讀性的css方法:
characters = @doc.css("sitcoms name")
# => ["<name>Married with Children</name>", "<name>Perfect Strangers</name>"]
當(dāng)已知查詢結(jié)果唯一時(shí),如果想直接返回這個(gè)結(jié)果,而不是列表,可以直接使用at_xpath或at_css:
@doc.css("dramas name").first # => "<name>The A-Team</name>"
@doc.at_css("dramas name") # => "<name>The A-Team</name>"
三、Namespaces
對(duì)于有多個(gè)標(biāo)簽的情況,命名空間就起到非常大的作用了.
例如有這樣一個(gè)parts.xml文件:
<parts> <!-- Alice's Auto Parts Store --> <inventory xmlns="http://alicesautoparts.com/"> <tire>all weather</tire> <tire>studded</tire> <tire>extra wide</tire> </inventory> <!-- Bob's Bike Shop --> <inventory xmlns="http://bobsbikes.com/"> <tire>street</tire> <tire>mountain</tire> </inventory> </parts>
可以使用唯一的URL作為namespaces,以區(qū)分不同的tires標(biāo)簽:
@doc = Nokogiri::XML(File.read("parts.xml"))
car_tires = @doc.xpath('//car:tire', 'car' => 'http://alicesautoparts.com/')
bike_tires = @doc.xpath('//bike:tire', 'bike' => 'http://bobsbikes.com/')
為了讓namespace的使用更方便,nokogiri會(huì)自動(dòng)綁定在根結(jié)點(diǎn)上找到的合適的任何namespace.
nokogiri會(huì)自動(dòng)關(guān)聯(lián)提供的URL,這個(gè)慣例可以減少代碼量.
例如有這樣一個(gè)atom.xml文件:
<feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title> <link /> <updated>2003-12-13T18:30:02Z</updated> <author> <name>John Doe</name> </author> <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id> <entry> <title>Atom-Powered Robots Run Amok</title> <link /> <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> <updated>2003-12-13T18:30:02Z</updated> <summary>Some text.</summary> </entry> </feed>
遵循上面提到的慣例,xmlns已被自動(dòng)綁定,不用再手動(dòng)為xmlns賦值:
@doc.xpath('//xmlns:title')
# => ["<title>Example Feed</title>", "<title>Atom-Powered Robots Run Amok</title>"]
同樣情況,css的用法:
@doc.css('xmlns|title')
并且在使用css方式時(shí),如果namespaces名字是xmlns,那么連這個(gè)詞本身都可以忽略掉:
@doc.css('title')
相關(guān)文章
Luhn算法學(xué)習(xí)及其Ruby版實(shí)現(xiàn)代碼示例
Luhn算法主要北用來進(jìn)行數(shù)字驗(yàn)證,尤其是卡號(hào)身份證號(hào)等,這里我們就來看一下Luhn算法學(xué)習(xí)及其Ruby版實(shí)現(xiàn)代碼示例:2016-05-05
Ruby實(shí)現(xiàn)的最短編輯距離計(jì)算方法
這篇文章主要介紹了Ruby實(shí)現(xiàn)的最短編輯距離計(jì)算方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05
淺談Ruby on Rails下的rake與數(shù)據(jù)庫數(shù)據(jù)遷移操作
Rails中的Migration相對(duì)來說更適合做數(shù)據(jù)庫的對(duì)象集合操作,而自動(dòng)化的rake則是一個(gè)較好的選擇,下面來淺談Ruby on Rails下的rake與數(shù)據(jù)庫數(shù)據(jù)遷移操作,需要的朋友可以參考下2016-06-06

