国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

Ruby使用REXML庫來解析xml格式數據的方法

2020-10-29 19:35:49
字體:
來源:轉載
供稿:網友

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

  • 100%用ruby編寫
  • 可以用來解析SAX和DOM
  • 輕量,不足2000行代碼
  • 提供完整的API支持
  • ruby中內置

下面我們來看看如何使用它,假設我們有如下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:

require 'rexml/document'include REXMLxmlfile = File.new("movies.xml")xmldoc = Document.new(xmlfile)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}

使用XPATH:

require 'rexml/document'include REXMLxmlfile = File.new("movies.xml")xmldoc = Document.new(xmlfile)movie = XPath.first(xmldoc, "http://movie")p movieXPath.each(xmldoc, "http://type") { |e| puts e.text }names = XPath.match(xmldoc, "http://format").map {|x| x.text }p names

以備不時之需!

PS:關于REXML的安全問題
Ruby官方網站在8月23日發布了安全通告:http://www.ruby-lang.org/en/news/2008/08/23/dos-vulnerability-in-rexml/,在Ruby當前使用的XML解析庫REXML在解析具有嵌套遞歸元素的XML文件的時候,將會出現拒絕服務攻擊的缺陷,導致服務器資源耗盡!
凡是在Rails應用程序當中使用到了XML文件解析功能的都存在上述缺陷,需要進行修復。在Rails當中的修復辦法如下:
1、Rails2.0.2和以前的老版本
下載修復文件,拷貝到RAILS_ROOT/lib目錄下,并且在environment.rb當中加入語句

require ‘rexml-expansion-fix'

2、Rails 2.1.0以上版本
下載修復文件,拷貝到RAILS_ROOT/config/initializers目錄下即可。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大安市| 龙南县| 萨迦县| 鄂托克旗| 瑞昌市| 漠河县| 石泉县| 根河市| 开原市| 克拉玛依市| 隆林| 宁晋县| 辽源市| 庄浪县| 合江县| 潢川县| 陇南市| 靖宇县| 潍坊市| 武山县| 遂溪县| 花莲市| 简阳市| 清苑县| 金坛市| 福清市| 黄冈市| 得荣县| 开平市| 惠安县| 肃宁县| 盐源县| 余姚市| 藁城市| 称多县| 方山县| 石门县| 马龙县| 普格县| 宁远县| 拜泉县|