文章摘要
REXML 是一个完全用 Ruby 编写的 XML 处理器,支持两种 API:DOM-like 和 SAX-like。DOM-like 方法一次读取整个文件并存储为树状结构,适合小文件;SAX-like 方法则边解析边读取文件,适合大数据场景。REXML 的特点包括:完全用 Ruby 编写、支持 SAX 和 DOM、代码量少于 2000 行、提供完整 API、内置在 Ruby 内置库中。 文章还展示了如何使用 REXML 进行 DOM 解析和 XPath 查询,例如通过 Document.new 创建文档对象,使用 elements 和 attributes 方法获取数据。此外,文章提到了 REXML 的安全问题:Ruby 官方在 2008 年 8 月 23 日发布漏洞公告,指出在解析嵌套递归 XML 文件时存在拒绝服务攻击风险。针对这一问题,Rails 应用程序需要进行修复: - 对于Rails 2.0.2 及以下版本,需在 RAILS_ROOT/lib 目录下下载修复文件并安装,同时在 environment.rb 中加入 require 'rexml-expansion-fix'; - 对于Rails 2.1.0 以上版本,直接在 RAILS_ROOT/config/initializers 目录下复制修复文件即可。 这篇文章为使用 REXML 解析 XML 文件提供了实用的入门指南,并提醒开发者注意安全问题。
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>
<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 REXML
xmlfile=File.new(“movies.xml”)
xmldoc=Document.new(xmlfile)
root=xmldoc.root
puts “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
}
include REXML
xmlfile=File.new(“movies.xml”)
xmldoc=Document.new(xmlfile)
root=xmldoc.root
puts “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 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
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
以备不时之需!
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目录下即可。
 您可能感兴趣的文章:Ruby中使用Nokogiri包来操作XML格式数据的教程Ruby中XML格式数据处理库REXML的使用方法指南实例解析Ruby程序中调用REXML来解析XML格式数据的用法Ruby程序中创建和解析XML文件的方法在Ruby中处理XML和XSLT以及XPath的简单教程Ruby的XML格式数据解析库Nokogiri的使用进阶 
© 版权声明
文章版权归作者所有,未经允许请勿转载。



