Ruby语言的运用可以帮助我们实现许多功能,比如这篇文章将为大家介绍的有关Ruby创建XML的相关实现方法,就是其中一种。#t#
环境配置:
先安装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"?>
- < !-- 书本信息 -->
- < 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>
以上就是Ruby创建XML的相关应用技巧讲解。