在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Ruby/ Ruby解析XML(REXML)
Ruby for循環(huán)
Ruby教程
Ruby文件I/O
Ruby迭代器
Ruby哈希
Ruby日期時間
Ruby類和對象
Ruby快速入門(30分鐘)
Ruby redo/retry語句
Ruby模塊
Ruby解析XML(REXML)
Ruby if-else語句
Ruby的功能特點
Ruby break/next語句
Ruby方法
Ruby是什么?
Ruby與Python比較
Ruby Case語句
Ruby目錄
Ruby范圍
Ruby異常
Ruby套接字編程(Socket)
Ruby字符串
Ruby安裝配置
Ruby運算符
Ruby while/do...while循環(huán)語句
Ruby第一個HelloWorld程序
Ruby until循環(huán)語句
Ruby注釋
Ruby塊
Ruby數(shù)據(jù)類型
Ruby面向?qū)ο?/span>
Ruby正則表達式
Ruby數(shù)組
Ruby變量
Ruby多線程編程

Ruby解析XML(REXML)

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)。

REXML功能:

  • 它100%使用Ruby語言編寫。
  • 它包含少于2000行代碼,因此更輕巧。
  • 它的方法和類很容易理解。
  • 它隨Ruby安裝一起提供,不需要單獨安裝。
  • 它用于DOM和SAX解析。

解析XML和訪問元素

現(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方法將IOString對象或Document作為參數(shù)。此參數(shù)指定必須讀取XML文檔的內(nèi)容。

如果Document構(gòu)造函數(shù)使用Document作為參數(shù),則將其所有元素節(jié)點克隆到新的Document對象。 如果構(gòu)造函數(shù)接受一個String參數(shù),則字符串將包含一個XML文檔。

XML 和 “Here Document”

這里文檔(“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>

在這里,在這里使用文檔信息。 包括<<EOFEOF之間的所有字符都是信息的一部分。

對于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   
}

Ruby XML DOM類似的解析

這里演示以樹形解析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   
}

Ruby XML以SAX類似的解析

這里演示以流的方式解析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)

上一篇:Ruby字符串下一篇:Ruby塊