XML是可擴展標記語言,是標準通用標記語言的一個子集,用于標記電子文件并使其結構化,本文是武林技術頻道介紹的在Ruby中處理XML和XSLT以及XPath的實例演示,一起來看看吧!
什么是 XML ?
XML 指可擴展標記語言(eXtensible Markup Language)。
可擴展標記語言,標準通用標記語言的子集,一種用于標記電子文件使其具有結構性的標記語言。
它可以用來標記數據、定義數據類型,是一種允許用戶對自己的標記語言進行定義的源語言。 它非常適合萬維網傳輸,提供統一的方法來描述和交換獨立于應用程序或供應商的結構化數據。
XML解析器結構和API
XML的解析器主要有DOM和SAX兩種。
Ruby 中解析及創建 XML
RUBY中對XML的文檔的解析可以使用這個庫REXML庫。
REXML庫是ruby的一個XML工具包,是使用純Ruby語言編寫的,遵守XML1.0規范。
在Ruby1.8版本及其以后,RUBY標準庫中將包含REXML。
REXML庫的路徑是: rexml/document
所有的方法和類都被封裝到一個REXML模塊內。
REXML解析器比其他的解析器有以下優點:
以下為實例的 XML 代碼,保存為movies.xml:
<collection shelf="New Arrivals"><movie title="Enemy Behind"> <type>War, Thriller</type> <format>DVD</format> <year>2003</year> <rating>PG</rating> <stars>10</stars> <description>Talk about a US-Japan war</description></movie><movie title="Transformers"> <type>Anime, Science Fiction</type> <format>DVD</format> <year>1989</year> <rating>R</rating> <stars>8</stars> <description>A schientific fiction</description></movie> <movie title="Trigun"> <type>Anime, Action</type> <format>DVD</format> <episodes>4</episodes> <rating>PG</rating> <stars>10</stars> <description>Vash the Stampede!</description></movie><movie title="Ishtar"> <type>Comedy</type> <format>VHS</format> <rating>PG</rating> <stars>2</stars> <description>Viewable boredom</description></movie></collection>
DOM 解析器
讓我們先來解析 XML 數據,首先我們先引入 rexml/document 庫,通常我們可以將 REXML 在頂級的命名空間中引入:
#!/usr/bin/ruby -w require 'rexml/document'include REXML xmlfile = File.new("movies.xml")xmldoc = Document.new(xmlfile) # 獲取 root 元素root = xmldoc.rootputs "Root element : " + root.attributes["shelf"] # 以下將輸出電影標題xmldoc.elements.each("collection/movie"){ |e| puts "Movie Title : " + e.attributes["title"]} # 以下將輸出所有電影類型xmldoc.elements.each("collection/movie/type") { |e| puts "Movie Type : " + e.text} # 以下將輸出所有電影描述xmldoc.elements.each("collection/movie/description") { |e| puts "Movie Description : " + e.text}
以上實例輸出結果為:
Root element : New ArrivalsMovie Title : Enemy BehindMovie Title : TransformersMovie Title : TrigunMovie Title : IshtarMovie Type : War, ThrillerMovie Type : Anime, Science FictionMovie Type : Anime, ActionMovie Type : ComedyMovie Description : Talk about a US-Japan warMovie Description : A schientific fictionMovie Description : Vash the Stampede!Movie Description : Viewable boredomSAX-like Parsing:
SAX 解析器
處理相同的數據文件:movies.xml,不建議SAX的解析為一個小文件,以下是個簡單的實例:
#!/usr/bin/ruby -w 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}" endend list = MyListener.newxmlfile = File.new("movies.xml")Document.parse_stream(xmlfile, list)
以上輸出結果為:
tag_start: "collection", {"shelf"=>"New Arrivals"}tag_start: "movie", {"title"=>"Enemy Behind"}tag_start: "type", {} text : "War, Thriller"tag_start: "format", {}tag_start: "year", {}tag_start: "rating", {}tag_start: "stars", {}tag_start: "description", {} text : "Talk about a US-Japan war"tag_start: "movie", {"title"=>"Transformers"}tag_start: "type", {} text : "Anime, Science Fiction"tag_start: "format", {}tag_start: "year", {}tag_start: "rating", {}tag_start: "stars", {}tag_start: "description", {} text : "A schientific fiction"tag_start: "movie", {"title"=>"Trigun"}tag_start: "type", {} text : "Anime, Action"tag_start: "format", {}tag_start: "episodes", {}tag_start: "rating", {}tag_start: "stars", {}tag_start: "description", {} text : "Vash the Stampede!"tag_start: "movie", {"title"=>"Ishtar"}tag_start: "type", {}tag_start: "format", {}tag_start: "rating", {}tag_start: "stars", {}tag_start: "description", {} text : "Viewable boredom"
XPath 和 Ruby
我們可以使用XPath來查看XML ,XPath 是一門在 XML 文檔中查找信息的語言(查看:XPath 教程)。
XPath即為XML路徑語言,它是一種用來確定XML(標準通用標記語言的子集)文檔中某部分位置的語言。XPath基于XML的樹狀結構,提供在數據結構樹中找尋節點的能力。
Ruby 通過 REXML 的 XPath 類支持 XPath,它是基于樹的分析(文檔對象模型)。
#!/usr/bin/ruby -w require 'rexml/document'include REXML xmlfile = File.new("movies.xml")xmldoc = Document.new(xmlfile) # 第一個電影的信息movie = XPath.first(xmldoc, "http://movie")p movie # 打印所有電影類型XPath.each(xmldoc, "http://type") { |e| puts e.text } # 獲取所有電影格式的類型,返回數組names = XPath.match(xmldoc, "http://format").map {|x| x.text }p names
以上實例輸出結果為:
<movie title='Enemy Behind'> ... </>War, ThrillerAnime, Science FictionAnime, ActionComedy["DVD", "DVD", "DVD", "VHS"]
XSLT 和 Ruby
Ruby 中有兩個 XSLT 解析器,以下給出簡要描述:
Ruby-Sablotron
這個解析器是由正義Masayoshi Takahash編寫和維護。這主要是為Linux操作系統編寫的,需要以下庫:
你可以在 Ruby-Sablotron 找到這些庫。
XSLT4R
XSLT4R 由 Michael Neumann 編寫。 XSLT4R 用于簡單的命令行交互,可以被第三方應用程序用來轉換XML文檔。
XSLT4R需要XMLScan操作,包含了 XSLT4R 歸檔,它是一個100%的Ruby的模塊。這些模塊可以使用標準的Ruby安裝方法(即Ruby install.rb)進行安裝。
XSLT4R 語法格式如下:
ruby xslt.rb stylesheet.xsl document.xml [arguments]
如果您想在應用程序中使用XSLT4R,您可以引入XSLT及輸入你所需要的參數。實例如下:
require "xslt" stylesheet = File.readlines("stylesheet.xsl").to_sxml_doc = File.readlines("document.xml").to_sarguments = { 'image_dir' => '/....' } sheet = XSLT::Stylesheet.new( stylesheet, arguments ) # output to StdOutsheet.apply( xml_doc ) # output to 'str'str = ""sheet.output = [ str ]sheet.apply( xml_doc )
上文是武林技術頻道介紹的在Ruby中處理XML和XSLT以及XPath的實例演示,希望對大家學習有幫助,也希望大家繼續支持武林技術頻道!
新聞熱點
疑難解答
圖片精選