Ruby方法與其他編程語言中的函數很像,Ruby方法用于將一個或多個重復語句捆綁到一個語句中實現,本文是武林技術頻道小編為大家帶來的Ruby的XML格式數據解析庫Nokogiri的使用進階,一起來了解一下吧!
一、基礎語法
1.直接以字符串形式獲取nokogiri對象:
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.也可以通過文件句柄獲取nokogiri對象:
f = File.open("blossom.xml")doc = Nokogiri::XML(f)f.close
3.還可以直接從網站獲取:
require 'open-uri'doc = Nokogiri::HTML(open("http://www.xxx.com/"))
二、XML文件解析實例
從XML/HTML文件里抓取字段的常用方法:
現在有一個名為shows.xml的文件,內容如下:
<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標簽的內容查找出來,可以這樣處理:
@doc = Nokogiri::XML(File.open("shows.xml"))@doc.xpath("http://character")
xpath和css方法,返回的是一個結點列表,類似于一個數組,它的內容就是從文件中查找出來的符合匹配規則的結點.
把dramas結點里的character結點列表查出來:
@doc.xpath("http://dramas//character")
更有可讀性的css方法:
characters = @doc.css("sitcoms name")# => ["<name>Married with Children</name>", "<name>Perfect Strangers</name>"]
當已知查詢結果唯一時,如果想直接返回這個結果,而不是列表,可以直接使用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
對于有多個標簽的情況,命名空間就起到非常大的作用了.
例如有這樣一個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,以區分不同的tires標簽:
@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會自動綁定在根結點上找到的合適的任何namespace.
nokogiri會自動關聯提供的URL,這個慣例可以減少代碼量.
例如有這樣一個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已被自動綁定,不用再手動為xmlns賦值:
@doc.xpath('//xmlns:title')# => ["<title>Example Feed</title>", "<title>Atom-Powered Robots Run Amok</title>"]
同樣情況,css的用法:
@doc.css('xmlns|title')
并且在使用css方式時,如果namespaces名字是xmlns,那么連這個詞本身都可以忽略掉:
@doc.css('title')
以上就是Ruby的XML格式數據解析庫Nokogiri的使用進階,你學會了嗎?相信這些內容正是大家最想了解的,如果你還想不斷的學習和進步,你可以來武林技術頻道好好學習。
新聞熱點
疑難解答
圖片精選