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

首頁 > 編程 > Ruby > 正文

實例解析Ruby程序中調用REXML來解析XML格式數據的用法

2020-02-24 15:39:15
字體:
來源:轉載
供稿:網友

Rexml不是Ruby唯一的XML庫,而nqxml也是用Ruby編寫的,但是XML解析器封裝了用C編寫的Jade庫,下面就跟著武林技術頻道小編來學習實例解析Ruby程序中調用REXML來解析XML格式數據的用法。

在他的 REXML 概述中,Russell 評論道:
我有這樣的問題:我不喜歡令人困惑的 API。有幾種用于 Java 實現的 XML 解析器 API。其中大多數都遵循 DOM 或 SAX,并且在基本原理上與不斷出現的眾多 Java API 非常相似。也就是說,它們看 上去象是由從未使用過他們自己的 API 的理論家設計出來的。 通常,現有的 XML API 都很令人討厭。他們采用一種被明確設計成非常簡單、一流且功能強大的標記語言, 然后用討厭的、過多的和大型 API 對它進行封裝。甚至是為了進行最基本的 XML 樹操作,我總是不得不參考 API 文檔; 沒有任何東西是憑直覺的,而且幾乎每個操作都很復雜。
雖然我并不認為它有多么令人心煩,但我同意 Russell 的觀點:XML API 對于大多數使用它們的人來說無疑帶來了過多的工作量。

示例
看下面的book.xml:

引用

<library shelf="Recent Acquisitions">  <section name="Ruby">   <book isbn="0672328844">   <title>The Ruby Way</title>   <author>Hal Fulton</author>   <description>    Second edition. The book you are now reading.    Ain't recursion grand?   </description>   </book>  </section>  <section name="Space">   <book isbn="0684835509">    <title>The Case for Mars</title>    <author>Robert Zubrin</author>    <description>Pushing toward a second home for the human     race.    </description>   </book>   <book isbn="074325631X">    <title>First Man: The Life of Neil A. Armstrong</title>    <author>James R. Hansen</author>    <description>Definitive biography of the first man on     the moon.    </description>   </book>  </section> </library>

1 Tree Parsing(也就是DOM-like)

我們需要require rexml/document 庫,并且include REXML :

ruby;">require 'rexml/document' include REXML  input = File.new("books.xml") doc = Document.new(input)  root = doc.root puts root.attributes["shelf"]  # Recent Acquisitions  doc.elements.each("library/section") { |e| puts e.attributes["name"] } # Output: # Ruby # Space  doc.elements.each("*/section/book") { |e| puts e.attributes["isbn"] } # Output: # 0672328844 # 0321445619 # 0684835509 # 074325631X  sec2 = root.elements[2] author = sec2.elements[1].elements["author"].text  # Robert Zubrin 

這里要注意的是xml中的屬性和值被表示為一個hash,因此我們能夠通過attributes[]來提取我們需要的值,元素的值還能通過類似于path的字符串或者整數來取得.其中用整數取的話,是1-based而不是0-based.

2? Stream Parsing(也就是SAX-like Parsing)

這邊使用了一個小技巧,那就是定義了一個listener 類,它將會在parse的時候被回調:

require 'rexml/document' require 'rexml/streamlistener' include REXML  class MyListener  include REXML::StreamListener  def tag_start(*args)  puts "tag_start: #{args.map {|x| x.inspect}.join(', ')}"  end   def text(data)  return if data =~ /^/w*$/  # whitespace only  abbrev = data[0..40] + (data.length > 40 ? "..." : "")  puts " text : #{abbrev.inspect}"  end end  list = MyListener.new source = File.new "books.xml" Document.parse_stream(source, list) 


這里介紹一下StreamListener 模塊,這個模塊它提供了幾個空的回調方法,因此你可以為了實現你自己的功能而覆蓋它.當parser 進入一個tag時,就會調用tag_start方法.而text方法也是類似的,他只不過是當讀取到數據時會被回調,它的輸出是這樣的:

tag_start: "library", {"shelf"=>"Recent Acquisitions"} tag_start: "section", {"name"=>"Ruby"} tag_start: "book", {"isbn"=>"0672328844"} tag_start: "title", {} text : "The Ruby Way" 


3 XPath

REXML通過XPath 類來提供Xpath的支持. 它也同時支持DOM-like和SAX-like .還是前面的那個xml文件,我們使用Xpath可以這樣做:

book1 = XPath.first(doc, "http://book") # Info for first book found p book1  # Print out all titles XPath.each(doc, "http://title") { |e| puts e.text }  # Get an array of all of the "author" elements in the document. names = XPath.match(doc, "http://author").map {|x| x.text } p names 

輸出是類似于下面的:

<book isbn='0672328844'> ... </> The Ruby Way The Case for Mars First Man: The Life of Neil A. Armstrong ["Hal Fulton", "Robert Zubrin", "James R. Hansen"] 

以上就是關于實例解析Ruby程序中調用REXML來解析XML格式數據的用法介紹,相信認真看完的朋友都已經懂了,看了小編的介紹之后趕快收藏起來吧!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 阜宁县| 凭祥市| 瓦房店市| 张家港市| 庆城县| 花莲县| 华坪县| 达拉特旗| 和平区| 泰来县| 九龙县| 京山县| 宁安市| 馆陶县| 汝阳县| 双江| 金湖县| 江门市| 绿春县| 芷江| 佛学| 武穴市| 土默特右旗| 普格县| 岳普湖县| 尉犁县| 普洱| 彭山县| 鄂托克前旗| 淳安县| 金华市| 自治县| 留坝县| 诏安县| 宜兰县| 松江区| 吐鲁番市| 大洼县| 伊吾县| 嘉禾县| 昆山市|