国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Ruby > 正文

Ruby的XML格式數(shù)據(jù)解析庫(kù)Nokogiri的使用進(jìn)階

2020-02-24 15:36:20
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Ruby方法與其他編程語(yǔ)言中的函數(shù)很像,Ruby方法用于將一個(gè)或多個(gè)重復(fù)語(yǔ)句捆綁到一個(gè)語(yǔ)句中實(shí)現(xiàn),本文是武林技術(shù)頻道小編為大家?guī)?lái)的Ruby的XML格式數(shù)據(jù)解析庫(kù)Nokogiri的使用進(jìn)階,一起來(lái)了解一下吧!
一、基礎(chǔ)語(yǔ)法
1.直接以字符串形式獲取nokogiri對(duì)象:

ruby;">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.也可以通過(guò)文件句柄獲取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)容查找出來(lái),可以這樣處理:

@doc = Nokogiri::XML(File.open("shows.xml"))@doc.xpath("http://character")

xpath和css方法,返回的是一個(gè)結(jié)點(diǎn)列表,類似于一個(gè)數(shù)組,它的內(nèi)容就是從文件中查找出來(lái)的符合匹配規(guī)則的結(jié)點(diǎn).

把dramas結(jié)點(diǎn)里的character結(jié)點(diǎn)列表查出來(lái):

@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')

以上就是Ruby的XML格式數(shù)據(jù)解析庫(kù)Nokogiri的使用進(jìn)階,你學(xué)會(huì)了嗎?相信這些內(nèi)容正是大家最想了解的,如果你還想不斷的學(xué)習(xí)和進(jìn)步,你可以來(lái)武林技術(shù)頻道好好學(xué)習(xí)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 江达县| 衡阳市| 松阳县| 偃师市| 达拉特旗| 进贤县| 茂名市| 庐江县| 乌拉特中旗| 满洲里市| 龙井市| 容城县| 南昌市| 分宜县| 醴陵市| 梁平县| 苍溪县| 甘谷县| 永德县| 广南县| 汉川市| 台安县| 宿迁市| 达拉特旗| 丹巴县| 青阳县| 白朗县| 利辛县| 阳新县| 华池县| 于田县| 开江县| 双流县| 松滋市| 将乐县| 弥渡县| 嘉善县| 正阳县| 正阳县| 化州市| 正阳县|