Ruby語言的運用可以幫助我們實現許多功能,比如這篇文章將為大家介紹的有關Ruby創建XML的相關實現方法,就是其中一種。
重要文章閱讀:Ruby入門--Linux/Windows下的安裝、代碼開發及Rails實戰 http://www.linuxidc.com/Linux/2014-04/100242.htm
Ruby on rails初體驗系列文章:
http://www.linuxidc.com/Linux/2014-04/100245.htm
http://www.linuxidc.com/Linux/2014-04/100246.htm
http://www.linuxidc.com/Linux/2014-04/100247.htm
環境配置:
先安裝ruby,再安裝builder
builder安裝方法: gem install builder
Ruby創建XML代碼示例:
- 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"?>
- < !-- 書本信息 -->
- <libraryshelf="Recent Acquisitions">
- <sectionname="ruby">
- <bookisbn="0672310001">
- <title>Programming Ruby< /title>
- <author>Yukihiro < /author>
- <description>Programming Ruby - The
Pragmatic Programmer's Guide
- < /description>
- < /book>
- < /section>
- < /library>
- <inspect/>
- #<IO:0x2a06ae8>
以上就是Ruby創建XML的相關應用技巧講解。