使用REXML解析xml

 1 Tree Parsing(也就是DOM-like) 

  我们需要require rexml/document 库,并且include REXML : require 'rexml/document' 

  include REXML 

  input = File.new("books.xml") 

  doc = Document.new(input) 

  root = doc.root 

  puts root.attributes["shelf"] # Recent Acquisitions 

  doc.elements.each("library/section") { |e| puts e.attributes["name"] } 

  # Output: 

  # Ruby 

  # Space 

  doc.elements.each("*/section/book") { |e| puts e.attributes["isbn"] } 

  # Output: 

  # 0672328844 

  # 0321445619 

  # 0684835509 

  # 074325631X 

  sec2 = root.elements[2] 

  author = sec2.elements[1].elements["author"].text # Robert Zubrin 这里要注意的是xml中的属性和值被表示为一个hash,因此我们能够通过attributes[]来提取我们需要的值,元素的值还能通过类似于 path的字符串或者整数来取得.其中用整数取的话,是1-based而不是0-based. 

  2 Stream Parsing(也就是SAX-like Parsing) 

  这边使用了一个小技巧,那就是定义了一个listener 类,它将会在parse的时候被回调: 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 =http://blog.soso.com/qz.q/~ /^/w*$/ # whitespace only 

  abbrev = data[0..40] + (data.length > 40 ? "..." : "") 

  puts " text : #{abbrev.inspect}" 

  end 

  end 

  list = MyListener.new 

  source = File.new "books.xml" 

  Document.parse_stream(source, list) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值