XML是可擴展的標記語言,如HTML。它允許程序員開發(fā)可以被其他應(yīng)用程序讀取的應(yīng)用程序,而不管使用的是什么操作系統(tǒng)和開發(fā)語言。
它可用于保存中小型數(shù)據(jù)量,不用在后端有任何基于SQL的技術(shù)。
REXML是一個純Ruby的XML處理器。 它表示一個完整的XML文檔,包括PI,doctype等。一個XML文檔有一個可以由root()訪問的單個子對象。 如果想要為創(chuàng)建的文檔提供XML聲明,則必須自己添加一個。 REXML文檔不為您寫入默認聲明。
REXML靈感來自于Java的Electric XML庫。 它的API易于使用,體積小巧,并遵循Ruby方法的方法命名和代碼流。
它支持樹和流文檔解析。 Steam解析比樹解析快1.5倍。 但是,在流解析中無法訪問某些功能(如XPath)。
現(xiàn)在,從解析XML文檔開始,下面是一個示例代碼:
require "rexml/document"
file = File.new( "trial-1.xml" )
doc = REXML::Document.new file
在上面的代碼中,第3行用于解析提供的文件。
示例
require 'rexml/document'
# file : rexml-example.rb
include REXML
file = File.new("trial-1.xml")
doc = Document.new(file)
puts docs
在上面的代碼中,require語句加載了REXML庫。 然后包括REXML表示不必使用像REXML:: Document這樣的名稱。創(chuàng)建了trial-1.xml文件。并將文檔顯示在屏幕上。
F:\worksp\ruby>ruby rexml-example.rb
<?xml version='1.0' encoding='UTF-8'?>
<root>
Hello, this is first REXML use.
</root>
F:\worksp\ruby>
Document.new方法將IO,String對象或Document作為參數(shù)。此參數(shù)指定必須讀取XML文檔的內(nèi)容。
如果Document構(gòu)造函數(shù)使用Document作為參數(shù),則將其所有元素節(jié)點克隆到新的Document對象。 如果構(gòu)造函數(shù)接受一個String參數(shù),則字符串將包含一個XML文檔。
這里文檔(“Here Document”)是一種指定文本塊,保留換行符,空格或使用文本標識的方法。
使用“<<”命令后跟令牌字符串構(gòu)建文檔。
在Ruby中,“<<”和令牌字符串之間不應(yīng)有空格。
實例
#!/usr/bin/env ruby
# file : rexml-heredoc.rb
require 'rexml/document'
include REXML
info = <<XML
<info>
<name>Maxsu</name>
<street>人民大道</street>
<city>海口</city>
<contact>9854126575</contact>
<country>中國</country>
</info>
XML
document = Document.new( info )
puts document
執(zhí)行上面代碼,得到以下結(jié)果 -
F:\worksp\ruby>ruby rexml-heredoc.rb
<info>
<name>Maxsu</name>
<street>人民大道</street>
<city>???lt;/city>
<contact>9854126575</contact>
<country>中國</country>
</info>
F:\worksp\ruby>
在這里,在這里使用文檔信息。 包括<<EOF和EOF之間的所有字符都是信息的一部分。
對于XML解析示例,使用以下XML文件代碼作為輸入:
執(zhí)行上面代碼,得到以下結(jié)果 -
#!/usr/bin/ruby -w
require 'rexml/document'
# file : rexml-newxml.rb
include REXML
xmlfile = File.new("trial-2.xml")
xmldoc = Document.new(xmlfile)
# Now get the root element
root = xmldoc.root
puts "Root element : " + root.attributes["shelf"]
# This will output all the cloth titles.
xmldoc.elements.each("collection/clothing"){
|e| puts "cloth Title : " + e.attributes["title"]
}
# This will output all the cloth types.
xmldoc.elements.each("collection/clothing/type") {
|e| puts "cloth Type : " + e.text
}
# This will output all the cloth description.
xmldoc.elements.each("collection/clothing/description") {
|e| puts "cloth Description : " + e.text
}
這里演示以樹形解析XML數(shù)據(jù)。 將以上文件trial.xml代碼作為輸入。
#!/usr/bin/ruby -w
require 'rexml/document'
include REXML
xmlfile = File.new("trial.xml")
xmldoc = Document.new(xmlfile)
# Now get the root element
root = xmldoc.root
puts "Root element : " + root.attributes["shelf"]
# This will output all the cloth titles.
xmldoc.elements.each("collection/clothing"){
|e| puts "cloth Title : " + e.attributes["title"]
}
# This will output all the cloth types.
xmldoc.elements.each("collection/clothing/type") {
|e| puts "cloth Type : " + e.text
}
# This will output all the cloth description.
xmldoc.elements.each("collection/clothing/description") {
|e| puts "cloth Description : " + e.text
}
這里演示以流的方式解析XML數(shù)據(jù)。 將文件trial.xml代碼作為輸入。 在這里將定義一個偵聽器類,其方法將被解析器的回調(diào)目標。
建議不要對小文件使用類似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}"
end
end
list = MyListener.new
xmlfile = File.new("trial.xml")
Document.parse_stream(xmlfile, list)