侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Ruby程序中创建和解析XML文件的方法

技术  /  管理员 发布于 6年前   283

使用builder创建XML

builder安装方法:

gem install builder
require 'builder'   x = Builder::XmlMarkup.new(:target => $stdout, :indent => 1) #":target =>$stdout"参数:指示输出内容将被写向标准输出控制台 #":indent =>1"参数:XML输出形式将被缩进一个空格字符x.instruct! :xml,:version =>'1.1',:encoding => 'gb2312' x.comment! "书本信息"  x.library("shelf" => "Recent Acquisitions") { x.section("name" => "ruby"){ x.book("isbn" => "0672310001"){ x.title "Programming Ruby"  x.author "Yukihiro " x.description "Programming Ruby - The Pragmatic Programmer's Guide" } } } 

p x #打印XML

Ruby创建XML输出结果:

< ?xml version="1.1" encoding="gb2312"?> < !-- 书本信息 --> < library shelf="Recent Acquisitions"> < section name="ruby"> < book isbn="0672310001"> < title>Programming Ruby< /title> < author>Yukihiro < /author> < description>Programming Ruby - The Pragmatic Programmer's Guide< /description> < /book> < /section> < /library> < inspect/> #< IO:0x2a06ae8> 

使用ReXML解析XML

REXML 是一个完全用ruby写的processor ,他有多种api,其中两个经典的api是通过DOM-like 和SAX-like 来进行区分的。第一种是将整个文件读进内存,然后存储为一个分层的形式(也就是一棵树了).而第二种是"parse as you go",当你的文件很大,并且内存受到限制的时候,比较适合用这种。

看下面的book.xml:

引用

<library shelf="Recent Acquisitions">   <section name="Ruby">     <book isbn="0672328844">     <title>The Ruby Way</title>     <author>Hal Fulton</author>     <description>       Second edition. The book you are now reading.       Ain't recursion grand?     </description>     </book>   </section>   <section name="Space">     <book isbn="0684835509">       <title>The Case for Mars</title>       <author>Robert Zubrin</author>       <description>Pushing toward a second home for the human         race.       </description>     </book>     <book isbn="074325631X">       <title>First Man: The Life of Neil A. Armstrong</title>       <author>James R. Hansen</author>       <description>Definitive biography of the first man on         the moon.       </description>     </book>   </section> </library>


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 =~ /^\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) 


这里介绍一下StreamListener 模块,这个模块它提供了几个空的回调方法,因此你可以为了实现你自己的功能而覆盖它.当parser 进入一个tag时,就会调用tag_start方法.而text方法也是类似的,他只不过是当读取到数据时会被回调,它的输出是这样的:

tag_start: "library", {"shelf"=>"Recent Acquisitions"} tag_start: "section", {"name"=>"Ruby"} tag_start: "book", {"isbn"=>"0672328844"} tag_start: "title", {}  text  :  "The Ruby Way" ......................................... 


3 XPath

REXML通过XPath 类来提供Xpath的支持. 它也同时支持DOM-like和SAX-like .还是前面的那个xml文件,我们使用Xpath可以这样做:

book1 = XPath.first(doc, "//book")  # Info for first book found p book1  # Print out all titles XPath.each(doc, "//title") { |e| puts e.text }  # Get an array of all of the "author" elements in the document. names = XPath.match(doc, "//author").map {|x| x.text } p names 


输出是类似于下面的:

<book isbn='0672328844'> ... </> The Ruby Way The Case for Mars First Man: The Life of Neil A. Armstrong ["Hal Fulton", "Robert Zubrin", "James R. Hansen"] 


  • 上一条:
    Ruby和Shell脚本实现判断成绩及格功能
    下一条:
    perl用{}修饰变量名的写法分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客