什么是 XML ?
XML 指可擴(kuò)展標(biāo)記語言(eXtensible Markup Language)。
可擴(kuò)展標(biāo)記語言,標(biāo)準(zhǔn)通用標(biāo)記語言的子集,一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)性的標(biāo)記語言。
它可以用來標(biāo)記數(shù)據(jù)、定義數(shù)據(jù)類型,是一種允許用戶對(duì)自己的標(biāo)記語言進(jìn)行定義的源語言。 它非常適合萬維網(wǎng)傳輸,提供統(tǒng)一的方法來描述和交換獨(dú)立于應(yīng)用程序或供應(yīng)商的結(jié)構(gòu)化數(shù)據(jù)。
XML解析器結(jié)構(gòu)和API
XML的解析器主要有DOM和SAX兩種。
Ruby 中解析及創(chuàng)建 XML
RUBY中對(duì)XML的文檔的解析可以使用這個(gè)庫(kù)REXML庫(kù)。
REXML庫(kù)是ruby的一個(gè)XML工具包,是使用純Ruby語言編寫的,遵守XML1.0規(guī)范。
在Ruby1.8版本及其以后,RUBY標(biāo)準(zhǔn)庫(kù)中將包含REXML。
REXML庫(kù)的路徑是: rexml/document
所有的方法和類都被封裝到一個(gè)REXML模塊內(nèi)。
REXML解析器比其他的解析器有以下優(yōu)點(diǎn):
以下為實(shí)例的 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 數(shù)據(jù),首先我們先引入 rexml/document 庫(kù),通常我們可以將 REXML 在頂級(jí)的命名空間中引入:
#!/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"] # 以下將輸出電影標(biāo)題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}
以上實(shí)例輸出結(jié)果為:
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 解析器
處理相同的數(shù)據(jù)文件:movies.xml,不建議SAX的解析為一個(gè)小文件,以下是個(gè)簡(jiǎn)單的實(shí)例:
#!/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)
以上輸出結(jié)果為:
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(標(biāo)準(zhǔn)通用標(biāo)記語言的子集)文檔中某部分位置的語言。XPath基于XML的樹狀結(jié)構(gòu),提供在數(shù)據(jù)結(jié)構(gòu)樹中找尋節(jié)點(diǎn)的能力。
Ruby 通過 REXML 的 XPath 類支持 XPath,它是基于樹的分析(文檔對(duì)象模型)。
#!/usr/bin/ruby -w require 'rexml/document'include REXML xmlfile = File.new("movies.xml")xmldoc = Document.new(xmlfile) # 第一個(gè)電影的信息movie = XPath.first(xmldoc, "http://movie")p movie # 打印所有電影類型XPath.each(xmldoc, "http://type") { |e| puts e.text } # 獲取所有電影格式的類型,返回?cái)?shù)組names = XPath.match(xmldoc, "http://format").map {|x| x.text }p names
以上實(shí)例輸出結(jié)果為:
<movie title='Enemy Behind'> ... </>War, ThrillerAnime, Science FictionAnime, ActionComedy["DVD", "DVD", "DVD", "VHS"]
XSLT 和 Ruby
Ruby 中有兩個(gè) XSLT 解析器,以下給出簡(jiǎn)要描述:
Ruby-Sablotron
這個(gè)解析器是由正義Masayoshi Takahash編寫和維護(hù)。這主要是為L(zhǎng)inux操作系統(tǒng)編寫的,需要以下庫(kù):
你可以在 Ruby-Sablotron 找到這些庫(kù)。
XSLT4R
XSLT4R 由 Michael Neumann 編寫。 XSLT4R 用于簡(jiǎn)單的命令行交互,可以被第三方應(yīng)用程序用來轉(zhuǎn)換XML文檔。
XSLT4R需要XMLScan操作,包含了 XSLT4R 歸檔,它是一個(gè)100%的Ruby的模塊。這些模塊可以使用標(biāo)準(zhǔn)的Ruby安裝方法(即Ruby install.rb)進(jìn)行安裝。
XSLT4R 語法格式如下:
ruby xslt.rb stylesheet.xsl document.xml [arguments]
如果您想在應(yīng)用程序中使用XSLT4R,您可以引入XSLT及輸入你所需要的參數(shù)。實(shí)例如下:
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 )
新聞熱點(diǎn)
疑難解答
圖片精選