ruby学习系列--创建XML
环境配置:
先安装ruby,再安装builder
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
输出结果:
<?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>