Ruby的XML格式數(shù)據(jù)解析庫Nokogiri的使用進(jìn)階
一、基礎(chǔ)語法
1.直接以字符串形式獲取nokogiri對象:
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對象:
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
對于有多個(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)文章
Ruby設(shè)計(jì)模式編程中使用Builder建造者模式的實(shí)例
這篇文章主要介紹了Ruby設(shè)計(jì)模式編程中使用Builder建造者模式的實(shí)例,建造者模式將一個(gè)復(fù)雜對象的構(gòu)造與它的表示分離,使同樣的構(gòu)建過程可以創(chuàng)建不同的表示,需要的朋友可以參考下2016-03-03Ruby使用GDBM操作DBM數(shù)據(jù)存儲(chǔ)方法實(shí)例詳解
這篇文章主要介紹了Ruby使用GDBM操作DBM數(shù)據(jù)存儲(chǔ)方法實(shí)例詳解,需要的朋友可以參考下2022-04-04Ruby中使用Block、Proc、lambda實(shí)現(xiàn)閉包
這篇文章主要介紹了Ruby中使用Block、Proc、lambda實(shí)現(xiàn)閉包,在編程領(lǐng)域我們可以通俗的說:子函數(shù)可以使用父函數(shù)中的局部變量,這種行為就叫做閉包,需要的朋友可以參考下2014-06-06Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法
這篇文章主要介紹了Ruby實(shí)現(xiàn)的最優(yōu)二叉查找樹算法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-05-05