今天做的一个ruby解析xml的小程序
来源:互联网 发布:苹果越狱软件 编辑:程序博客网 时间:2024/05/17 03:10
发现用ruby写东西真的很享受,把api打开,仔细查看一下,几行代码就可以搞定了!我采用的是ruby中的libxml-ruby 来解析的,据说速度是最快的,我也没验证,大家有空可以去验证一下,用ruby的Benchmark.measure 验证,目前只是写的一个很简单的解析,不是通用类的那种,至少连封装都没有做,不过封装很简单了,呵呵,以下是代码:
%w[rubygems libxml xml].each { |x11| require x11 }
doc = XML::Document.file("posts.xml")
nodes = doc.find("posts")
#puts nodes.length
#puts nodes[0]
nodes.each do |node|
#puts node
if node[:user] == 'jnunemaker1'
itemnodes = node.find("post")
if itemnodes
itemnodes.each do |itemnode|
puts 'href: '+itemnode[:href]
puts 'hash: '+ itemnode[:hash]
puts 'description: '+ itemnode[:description]
puts 'tag: '+ itemnode[:tag]
puts 'time: '+ itemnode[:time]
puts 'others: '+ itemnode[:others]
puts'*************'
end
end
end
end
对应的解析的xml文件代码
<message>
<posts user="jnunemaker1" tag="ruby">
<post href="http://roxml.rubyforge.org/" hash="19bba2ab667be03a19f67fb67dc56917" description="ROXML - Ruby Object to XML Mapping Library" tag="ruby xml gems mapping" time="2008-08-09T05:24:20Z" others="56" extended="ROXML is a Ruby library designed to make it easier for Ruby developers to work with XML. Using simple annotations, it enables Ruby classes to be custom-mapped to XML. ROXML takes care of the marshalling and unmarshalling of mapped attributes so that developers can focus on building first-class Ruby classes."/>
<post href="http://code.google.com/p/sparrow/" hash="1df8a7cb9e8960992556518c0ea0d146" description="sparrow - Google Code" tag="ruby sparrow memcache queue" time="2008-08-06T15:07:24Z" others="115" extended="Sparrow is a really fast lightweight queue written in Ruby that speaks memcache. That means you can use Sparrow with any memcached client library (Ruby or otherwise)."/>
</posts>
<!-- fe04.api.del.ac4.yahoo.net uncompressed/chunked Sat Aug 9 00:20:11 PDT 2008 -->
<posts user="jnunemaker2" tag="ruby">
<post href="http://labs.reevoo.com/plugins/beanstalk-messaging" hash="d100c10208acbf5e954320a5577838d9" description="reevoolabs - Beanstalk Messaging" tag="railstips messaging queue rails ruby" time="2008-07-28T02:57:00Z" others="33" extended="Good write up on beanstalk"/>
<post href="http://www.slideshare.net/guest807bb2/rubyfringe?src=embed" hash="c3dc3b940dbe25e39737240b4e1ab071" description="Rockstar Memcached" tag="memcached performance caching ruby rails railstips" time="2008-07-28T02:30:50Z" others="11" extended="Killer presentation on memcached by Tobi of Shopify."/>
<post href="http://www.igvita.com/2008/07/22/unix-signals-for-live-debugging/" hash="288054a38d870b15bdf060ed5c6b2a2e" description="Unix Signals for Live Debugging - igvita.com" tag="ruby signals unix debugging signal railstips" time="2008-07-27T04:53:00Z" others="86" extended="I've known how to kill processes and such but never quite understood kill. Ilya Grigorik explains not only how to send those signals but how to use them in your scripts to change the way they behave on the fly. Very cool."/>
</posts>
</message>
- 今天做的一个ruby解析xml的小程序
- 今天做了一个系统托盘的小程序
- 今天写的xml的小程序
- DOM4J解析XML 写的一个小程序总结
- 今天做了一个简单的FLASH菜单定位相应栏目的一段小程序。
- 今天搞定的一个小例子程序
- 今天完成的一个小程序
- 今天写了一个C#的小程序,使用了文件操作,正则表达式和XML
- 今天做的小程序 --10.30
- 用VB做的一个小程序
- 一个Ruby小程序
- 今天起,做一个对得起自己专业的程序猿!
- 一个SAX解析xml的简单程序。
- 一个C++的 XML 解析程序
- 一个C++的 XML 解析程序
- 一个解析配置文件的小程序
- 今天学结构体写的一个小程序
- 今天写的小程序
- 求解线性方程组--全主元消去法
- 一个致命的错误
- 导出Excel 常用属性(2)
- Sql Server 性能分析1 –查看数据库的相关信息
- DCME
- 今天做的一个ruby解析xml的小程序
- GOF 设计模式之 Command
- 报错无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启
- Opera 10 Slated For September 1 Launch. Will Anyone Take Notice?
- Numerical Brain practice
- 门面模式
- 《如何阅读一本书》读书笔记 part2
- 有关MYSQLDUMP的详细解释
- Xml WebService完全实例解析(一)