麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Ruby > 正文

Ruby程序中創建和解析XML文件的方法

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

XML是一種可擴展的標記語言,它允許程序員開發可以被其他應用程序讀取的應用程序,而不管所使用的操作系統和開發語言是什么,那么Ruby程序中創建和解析XML文件的方法有哪些呢?武林技術頻道帶你了解。

使用builder創建XML

builder安裝方法:

ruby.html="" tags="">ruby;">gem install builder
require 'builder'   x = Builder::XmlMarkup.new(:target => $stdout, :indent => 1) #":target =>$stdout"參數:指示輸出內容將被寫向標準輸出控制臺 #":indent =>1"參數:XML輸出形式將被縮進一個空格字符x.instruct! :xml,:version =>'1.1',:encoding => 'gb2312' x.comment! "書本信息"  x.library("shelf" => "Recent Acquisitions") { x.section("name" => "ruby"){ x.book("isbn" => "0672310001"){ x.title "Programming Ruby"  x.author "Yukihiro " x.description "Programming Ruby - The Pragmatic Programmer's Guide" } } } 

p x #打印XML

Ruby創建XML輸出結果:

< ?xml version="1.1" encoding="gb2312"?> < !-- 書本信息 --> < library shelf="Recent Acquisitions"> < section name="ruby"> < book isbn="0672310001"> < title>Programming Ruby< /title> < author>Yukihiro < /author> < description>Programming Ruby - The Pragmatic Programmer's Guide< /description> < /book> < /section> < /library> < inspect/> #< IO:0x2a06ae8> 

使用ReXML解析XML

REXML 是一個完全用ruby寫的processor ,他有多種api,其中兩個經典的api是通過DOM-like 和SAX-like 來進行區分的。第一種是將整個文件讀進內存,然后存儲為一個分層的形式(也就是一棵樹了).而第二種是"parse as you go",當你的文件很大,并且內存受到限制的時候,比較適合用這種。

看下面的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 :

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程序中創建和解析XML文件的方法,希望可以幫助您全面了解編程的相關知識。

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

圖片精選

主站蜘蛛池模板: 在线播放中文 | av日韩在线免费观看 | 人人做人人看 | 久久综合爱 | 99国产精品白浆在线观看免费 | 斗破苍穹在线观看免费完整观看 | 精品黑人一区二区三区国语馆 | 国产精品视频在 | 亚洲成人在线免费观看 | 黄色av网 | 久草在线观看资源 | 色淫网站免费视频 | 欧洲精品久久 | 久久影院国产精品 | 黄色网战入口 | 黄色一级片在线观看 | 在线高清中文字幕 | 亚洲片在线观看 | 黄色免费av网站 | 黄色片网站在线免费观看 | 斗罗破苍穹在线观看免费完整观看 | 久久久久久久久久久影视 | 黄视频网站免费 | 亚洲国产精品久久久久久久久 | 操你啦免费视频 | 久久精品视频1 | 日本精品久久久一区二区三区 | 综合在线一区 | 久久精品日韩一区 | 欧美日韩免费一区二区三区 | 久久久久一区二区三区四区五区 | 国产一区二区久久精品 | 久久久久北条麻妃免费看 | 欧美日韩精品一区二区三区蜜桃 | 久草欧美 | 韩国草草影院 | 91精品视频免费 | 国产在线精品一区二区三区不卡 | 羞羞草视频 | 久久伊人国产精品 | 红杏网站永久免费视频入口 |